I have been working on trying to figure this out for several days. Google has many answers, but none of them seem to solve this problem. I was hoping someone else had this problem and know what to do to fix it.
So, the problem: I would like to delete files older than 3 days using Cron.
My crontab:
# mh dom mon dow command SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin */1 * * * * /root/insert.sh 0 0 * * * /root/backup.sh */1 * * * * find /root/backups -mtime +3 -exec /bin/rm {} \;
With this, /root/backup.sh works at midnight every night, and it works. Also /root/insert.sh (runs a PHP script to move some data to another folder) works every minute.
I went to / root / backups and manually typed find /root/backups -mtime +15 -exec rm {} \; that worked. He deleted all files older than 15 days.
To confirm that cron runs this line of code. I ran tail -f /var/log/syslog | grep CRON tail -f /var/log/syslog | grep CRON (I am running ubuntu 13.04 server)
Jul 24 07:47:01 myServer CRON[13934]: (root) CMD (find /root/backups -mtime +3 -exec rm {} \;) Jul 24 07:48:01 myServer CRON[13937]: (root) CMD (/root/insert.sh) Jul 24 07:48:01 myServer CRON[13938]: (root) CMD (find /root/backups -mtime +3 -exec rm {} \;) Jul 24 07:49:01 myServer CRON[13954]: (root) CMD (/root/insert.sh) Jul 24 07:49:01 myServer CRON[13955]: (root) CMD (find /root/backups -mtime +3 -exec /bin/rm {} \;)
So you can see that it works. I tried to put PATH in my crontab. I tried putting /bin/rm instead of rm . Someone suggested making sure that the end of crontab has CR. He does.
Despite the fact that he root cron made chmod a+rwx backups , each user can change this folder. Still out of luck.
I tried rm -fr /root/backups and /bin/rm -fr /root/backups in my crontab and this did not work. If I execute /bin/rm -fr /root/backups >> /root/logmeplease.log 2>&1 and nothing is logged.
Finally, I tried to put this in a shell script.
#!/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games find /root/backups -mtime +5 -exec /bin/rm {} \;
Again it has a CR at the bottom of the file. This script works by running manually, but cron will not execute it. I also tried #!/bin/bash -l and #!/bin/bash -x at the top.
Is there anything really simple, I donβt know why my root cron will not delete files? And syslog shows that it is running a script or command?
Thanks for any help with this!