LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cron job to delete regular files (https://www.linuxquestions.org/questions/linux-newbie-8/cron-job-to-delete-regular-files-4175597999/)

chtsalid 01-22-2017 05:41 AM

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!

Turbocapitalist 01-22-2017 05:52 AM

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.

chtsalid 01-22-2017 05:57 AM

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!

Turbocapitalist 01-22-2017 05:59 AM

Quote:

Originally Posted by chtsalid (Post 5658541)
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!

I wouldn't expect it to. ;) Find where find is on your system and use that path instead of /bin:

Code:

which find
Also instead of -exec /bin/rm {} \; try just -delete instead.

It will be easier if you work it out in an interactive shell first before adding it to cron

chtsalid 01-22-2017 06:12 AM

Hi,

still not working

[root@rh2 tmp]# which find
/bin/find

11 13 * * * root /bin/find /home/mike/tmp -type f -name mike* -delete

:)

Turbocapitalist 01-22-2017 06:16 AM

Quote:

Originally Posted by chtsalid;
11 13 * * * root /bin/find /home/mike/tmp -type f -name mike* -delete

Ok. You have the right path. Now you've changed the formula by adding -name. That's great, but you'll need to wrap the name in quotes so that the asterisk is processed by find and not your shell. Without quotes, the shell applies globbing

Code:

/bin/find /home/mike/tmp -type f -name 'mike*' -delete
Also what method are you using to create the cronjob? sudo crontab -e or some other method?

chtsalid 01-22-2017 06:20 AM

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!

Turbocapitalist 01-22-2017 06:26 AM

Does the following work in the shell?

Code:

/bin/find /home/mike/tmp -type f -name 'mike*' -delete
See about getting that working before moving that to cron.

chtsalid 01-22-2017 06:29 AM

Yes, it works, it deleted all files.

Where does the problem lie then?

Thanks!

Turbocapitalist 01-22-2017 06:35 AM

If there's a problem it will usually show up in the logs. Which distro are you running, including version?

chtsalid 01-22-2017 06:44 AM

I am using CentOS 7.

michaelk 01-22-2017 09:27 AM

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.

chtsalid 01-22-2017 11:00 AM

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!

Turbocapitalist 01-22-2017 11:33 AM

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.

chtsalid 01-22-2017 11:35 AM

Thank you for your input!


All times are GMT -5. The time now is 12:57 AM.