Due to some bug in one of my scripts i'm being forced to delete temp files every 30 minutes until i'll figure it out and debug it.

To get it done i've written a small bash script that i run every 30 minutes (tempCleaner)

numberOfFiles=$(find -name php* | wc -l)
echo "$numberOfFiles"
if (("$numberOfFiles" <= "0"))
                echo "Nothing to delete"
                echo "There's nothing to delete, everything is fine $(date)" >> /tmp/scan_log
                echo "Something to delete"
                echo "Oops, there are some files that needs to be deleted $(date)" >> /tmp/scan_log
                sudo rm -vrf php* >> /tmp/deleted_files_log

i've tested it out (./tempCleaner) and it worked just fine now to run it every 30 minutes i'm using cron (sudo crontab -e) and wrote the following: (I'm aware of the time, it's just to test it up rapidly)

*       *       *       *       *       nice -19 /tmp/tempCleaner >> /tmp/error_log_cron

no matter what i've tried, it's not working for me. why ? what am i doing wrong ? what am i missing ?

Some technical information, The server load is oftenly over 1.0 (not sure if it matters to cron or not) the files are located in /tmp, the files that i want to delete are related to the following group www-data:www-data the bash file (tempCleaner) is set +x and is related to ubuntu:ubuntu


The script works in the directory you start it in, which is almost certainly something else in the cron environment. Add a statement to cd to the directory in question before doing anything else.


Adding cd /tmp should work. Something else might be to quote the -name option for find:

cd /tmp
echo $(date) >> /tmp/deleted_files
find -name 'php*' -type f | xargs rm -rvf >> /tmp/deleted_files 

And another edit: It might be that nice is not in the path of your cron environment. Either call the script directly (which makes sense while debugging anyway) or add the full path to nice.

| improve this answer | |
  • I'm not sure i understand you right.. i've tried to add cd /tmp to the head of the script but it didn't change anything. – user148366 Feb 1 '13 at 14:02
  • See my edit with a variant of your script (that works here). – Sven Feb 1 '13 at 14:17

You should probably put the whole path before the commands, like find and rm.




in my case...


P.S. if you're doing the sudo inside a script, what happens if it prompts you for a password??

Sudo adding the script to crontab should drop the cron into root, and the sudo before rm shouldn't be necessary..

| improve this answer | |

What @svenW said is correct.. but in your find you should add something like -mmin +10 to make sure you aren't deleting an in-use file that will cause your request to like 500 or something on a user.

| improve this answer | |
  • That's not the case as it's controlled environment and i've created dummy php$RANDOM$RANDOM files that should have been deleted. – user148366 Feb 1 '13 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy