Failed to delete files using cron

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!

+4
source share
2 answers

to be more reliable, you have to write find ... in a separate script file. eg.

 #! /bin/bash /usr/bin/find .... -delete \; 

To test the script, run it without the environment, i.e. become a superuser and run

 env -i myCronScript 

and see what happens. These are the conditions under which cron runs your script.

+2
source

cron uses sh by default and checks for aliases on this shell. You may find that the rm command is forced to request promt when using sh-shell:

 root@server1 ]# env | grep SHELL SHELL=/bin/sh [ root@server1 ]# alias ... alias rm='rm -i' 

This is why a change to bash might help, because there are no such aliases.

+1
source

Source: https://habr.com/ru/post/1493205/


All Articles