Crontab to clear tmp files


#1

I am trying to write a crontab that will clear all of my non-session files out of the /tmp directory every hour: Here is the contents of my crontab file:
17 * * * * /home//hourly/clear_tmp.sh

Inside of clear_tm.sh I have:
#!/bin/bash
rm -f $(find /tmp -type f | grep -v sess_);

Im a little bit of a newbie with the crontabs, can someone let me know where my problem is? Thanks in advance.


#2

Is your .sh script file executable with a chmod of 755?

What error message do you get?


Norm

Opinions are my own views and not the views of DreamHost. :open_mouth:
Any advice offered by me should be acted upon at your own risk.


#3

Yes, it is set to 755. I am actually not getting an error message, but I am still seeing files in the tmp directory that are several hours old, indicating that the script isnt working properly.


#4

Are you putting /tmp as in the system tmp or your own user /tmp as in say /home/username/tmp?
The crontab will be looking in the system /tmp folder unless you give the full path.

Later.
Is that just a typo btw in your OP where you left the ‘p’ off tmp?


Norm

Opinions are my own views and not the views of DreamHost. :open_mouth:
Any advice offered by me should be acted upon at your own risk.


#5

Have you run this script manually with any success?

-Scott


#6

Its odd, it seems as though its not working now, but it had been earlier in the week (or at least I wasnt seeing any tmp files doing the same activity that usually creates them). Does anybody whose a better shell scripter than myself see a problem with the script?


#7

I use xargs for operations like this. I use -user so I ignore everybody else’s files. I also use full paths since cron doesn’t set all your paths. This is how I’d do it:

#!/bin/bash
/usr/bin/find /tmp -user USERNAME -type f | /bin/grep -v sess_ | /usr/bin/xargs /bin/rm -f

-Scott


#8

Thanks Scott, I used that and it works perfectly now, thanks so much for your help.