0

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)

#!/bin/bash
numberOfFiles=$(find -name php* | wc -l)
echo "$numberOfFiles"
if (("$numberOfFiles" <= "0"))
        then
                echo "Nothing to delete"
                echo "There's nothing to delete, everything is fine $(date)" >> /tmp/scan_log
        else
                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
fi

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

3 Answers3

3

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.

Edit:

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

#!/bin/bash 
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.

Sven
  • 98,649
  • 14
  • 180
  • 226
  • 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. –  Feb 01 '13 at 14:02
  • See my edit with a variant of your script (that works here). – Sven Feb 01 '13 at 14:17
1

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

/usr/bin/find

and

/bin/rm

in my case...

Nick

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..

NickW
  • 10,263
  • 1
  • 20
  • 27
0

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.

Mike
  • 22,310
  • 7
  • 56
  • 79
  • That's not the case as it's controlled environment and i've created dummy php$RANDOM$RANDOM files that should have been deleted. –  Feb 01 '13 at 14:07