Reputation: 41
I've been working on trying to figure this out for days. Google has many answers but none of them seem to solve this problem. I was hoping someone else has had this issue and knows what to do to fix it.
So the problem: I would like to delete files older than 3 days using Cron.
My Crontab:
# m h 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 Crontab /root/backup.sh
runs at midnight every night and this 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. It deleted all the files older than 15 days.
To confirm that cron is running this line of code. I ran tail -f /var/log/syslog | grep CRON
(I'm running ubuntu server 13.04)
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 is running. I've tried putting a PATH in my crontab. I've tried putting /bin/rm
instead of rm
. Someone has suggested making sure the end of the crontab has a CR. It does.
Even though it's root's cron I did chmod a+rwx backups
so anyone can modify this folder. Still no luck there.
I've tried rm -fr /root/backups
and /bin/rm -fr /root/backups
in my crontab and that hasn't worked. If I do /bin/rm -fr /root/backups >> /root/logmeplease.log 2>&1
and nothing get's logged.
Lastly, I've tried putting this into 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 this has a CR at the bottom of the file. This script works by running manually but cron won't execute it. I've also tried #!/bin/bash -l
and #!/bin/bash -x
at the top.
Is there something really simple I'm missing as to why my root's cron won't delete any files? And the syslog shows that it is running the script, or command?
Thanks for any help on this!
Upvotes: 4
Views: 6804
Reputation: 797
Dear Crontab uses sh shell instead of bash by default. you can use /dev/null to empty files. check below script. it will help.
#!/bin/bash
tar=$(ls -lt /backup/manual | grep ^d | tail -n1 | awk '{print $NF}')
for i in $(find /backup/manual/"$tar" -type f )
do cat /dev/null > "$i"
done > /dev/null 2>&1 && mv /backup/manual/"$tar" /tmp/
explanation: kindly use for loop to reach all files from targeted folders and make them null and move to /tmp directory at last
Upvotes: 0
Reputation: 11
cron is using sh by default, and please check aliases on that shell. You might find that command rm is forced to request promt when using sh-shell:
root@server1]# env | grep SHELL
SHELL=/bin/sh
[root@server1]# alias
...
alias rm='rm -i'
That's why changing to bash might help, because there is no that kind of aliases.
Upvotes: 1
Reputation: 493
to be more robust you should write the find ... in a separate script file. e.g.
#! /bin/bash
/usr/bin/find .... -delete \;
To test the Script run it without environment i.e. become superuser and start
env -i myCronScript
and see what happens. These are the conditions a cron runs your script.
Upvotes: 2