Using Crontab in CentOS7

Issues related to applications and software problems
Post Reply
jesus.vm1
Posts: 2
Joined: 2021/11/13 00:13:52

Using Crontab in CentOS7

Post by jesus.vm1 » 2022/03/01 17:33:20

I have the next command to delete the files daily with more that 7 days.

24 9 * * 2 root find /tmp/LOGS3/*.json -mtime +7 -delete


But I need delete the files in the main path and subdirectoys in the path LOGS3, is possible?

User avatar
TrevorH
Site Admin
Posts: 33219
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Using Crontab in CentOS7

Post by TrevorH » 2022/03/01 17:42:32

Well, first I don't think you should really log to /tmp at all. That's not what it's for. The /var/log directory exists for a reason.

Second that crontab entry is of the format used in /etc/cron.* files and not for those using crontab -e but since you don't say which you are using, cannot tell if it is correct or not. The difference is that /etc/cron.* entries have a 6th field that is the username to run as where crontab -e entries are already per user and the field is not present and would try to execute the comamnd 'root'.

Next you probably want to quote your filename pattern as, certainly if you run that from a bash prompt, the * will get expanded into a list of all the files in the current working directory that match that pattern before the command is run.

Also, your crontab entry only runs on day 2 of the week (Tuesday) so it won't do this daily but weekly.

It's also good practice with cron jobs to give the full path to the command you want to run so not 'find' but '/usr/bin/find'
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

Whoever
Posts: 1361
Joined: 2013/09/06 03:12:10

Re: Using Crontab in CentOS7

Post by Whoever » 2022/03/01 20:40:33

jesus.vm1 wrote:
2022/03/01 17:33:20
I have the next command to delete the files daily with more that 7 days.

24 9 * * 2 root find /tmp/LOGS3/*.json -mtime +7 -delete


But I need delete the files in the main path and subdirectoys in the path LOGS3, is possible?
As well as the possible issues with the format of the cron command, how about using the package "tmpwatch" instead of rolling your own?

Post Reply