cron job to delete regular files
Hi,
I have created the following cron job 37 12 * * * root find /home/mike/tmp -type f -exec rm {} \; to delete files in /home/mike/tmp directory, however files are not deleted. [root@rh2 ~]# ls /home/mike/tmp mike1 mike2 mike3 Any idea? Many thanks! |
The path is different in cron, so you should use the full path to find. There is also the risk of trouble if you have weird file names such as those with whitespace. So I'd say try -delete instead of -exec
Also, how exactly have you created and stored the cron job? There are two ways, each with a slightly different format. |
Hi,
thanks for your help! I edited the /etc/crontab. I altered the config as following. 55 12 * * * root /bin/find /home/mike/tmp -type f -exec /bin/rm {} \; but still it does not work [root@rh2 tmp]# ls mike1 mike2 mike3 Thanks! |
Quote:
Code:
which find It will be easier if you work it out in an interactive shell first before adding it to cron |
Hi,
still not working [root@rh2 tmp]# which find /bin/find 11 13 * * * root /bin/find /home/mike/tmp -type f -name mike* -delete :) |
Quote:
Code:
/bin/find /home/mike/tmp -type f -name 'mike*' -delete |
I did, vi /etc/crontab
19 13 * * * root /bin/find /home/mike/tmp -type f -name 'mike*' -delete but still nothing. Should I put anything else after setting the -delete option? Thanks! |
Does the following work in the shell?
Code:
/bin/find /home/mike/tmp -type f -name 'mike*' -delete |
Yes, it works, it deleted all files.
Where does the problem lie then? Thanks! |
If there's a problem it will usually show up in the logs. Which distro are you running, including version?
|
I am using CentOS 7.
|
Look at the contents of /var/log/cron. To see the last 10 lines you can use the tail command i.e.
tail /var/log/cron (as root) I see nothing obviously wrong with your find command and it works on my CentOS 7 system. |
Hi,
thank you for your input! I found the mistake. It had nothing to do with all these :). timedatectl local time was wrong. I changed it, and it worked having the following command configured. [root@rh2 tmp]# crontab -l 56 17 * * * find /home/mike/tmp -type f -exec rm {} \; [root@rh2 tmp]# tail /var/log/cron Jan 22 17:55:17 rh2 crontab[17395]: (root) BEGIN EDIT (root) Jan 22 17:55:24 rh2 crontab[17395]: (root) REPLACE (root) Jan 22 17:55:24 rh2 crontab[17395]: (root) END EDIT (root) Jan 22 17:55:31 rh2 crontab[17398]: (root) LIST (root) Jan 22 17:55:52 rh2 crontab[17415]: (root) LIST (root) Jan 22 17:56:01 rh2 crontab[17417]: (root) BEGIN EDIT (root) Jan 22 17:56:01 rh2 crond[15011]: (root) RELOAD (/var/spool/cron/root) Jan 22 17:56:01 rh2 CROND[17422]: (root) CMD (find /home/mike/tmp -type f -exec rm {} \;) Jan 22 17:56:24 rh2 crontab[17417]: (root) REPLACE (root) Jan 22 17:56:24 rh2 crontab[17417]: (root) END EDIT (root) Thanks! |
Great. Be sure to run NTPd to keep the time synchronized, if you have time zone settled.
The -exec rm {} \; part is still problematic. You need to at least quote the {} part. Better would be to try the -delete option. |
Thank you for your input!
|
All times are GMT -5. The time now is 12:57 AM. |