crontab to clear tmp files

crontab to clear tmp files

Posted by: skb104
Posted on: 2006-06-18 09:34:00

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

Re: crontab to clear tmp files

Posted by: norm1037
Posted on: 2006-06-18 11:31:00

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.shocked
Any advice offered by me should be acted upon at your own risk.

Re: crontab to clear tmp files

Posted by: skb104
Posted on: 2006-06-18 11:42:00

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.

Re: crontab to clear tmp files

Posted by: norm1037
Posted on: 2006-06-18 11:49:00

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.shocked
Any advice offered by me should be acted upon at your own risk.
Edited by norm1037 on 06/18/06 11:59 AM (server time).

Re: crontab to clear tmp files

Posted by: sdayman
Posted on: 2006-06-18 13:29:00

Have you run this script manually with any success?

-Scott

Re: crontab to clear tmp files

Posted by: skb104
Posted on: 2006-06-18 19:47:00

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?

Re: crontab to clear tmp files

Posted by: sdayman
Posted on: 2006-06-18 20:24:00

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

Re: crontab to clear tmp files

Posted by: skb104
Posted on: 2006-06-19 18:23:00

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

Tags: tmp directorycrontab