Sunday, January 9, 2022

[SOLVED] crontab doesn't work after /etc/crontab is configured

Issue

I added this line to the end of /etc/crontab file:

*       *       *       *      *      cp /etc /backups -R > /dev/null && tar -Jcf /backups/etc-`date +\%Y-\%m-\%d-\%H-\%M-\%S`.tar.xz /backups/etc > /dev/null && rm -rf /backups/etc > /dev/null

and then I restarted the crond service with systemctl restart crond command but didn't work

It worked when I run this command cp /etc /backups -R > /dev/n...... in terminal

And my mailx for root is empty.

Could anyone tell me whats wrong with my configuration? Thank you very much!!!


Solution

/etc/crontab is not an ordinary crontab file; it's a system crontab file. Each line has the usual 5 fields specifying the schedule, then a 6th field specifying the account. With the line you show in /etc/crontab, it will attempt to run the command as user cp.

If you're using the Vixie cron implementation (you probably are), run man 5 crontab and search for "EXAMPLE SYSTEM CRON FILE".

I recommend not touching /etc/crontab. Rather use the crontab command to create a user crontab for whichever account you want. Run crontab as root if the command begin run needs root access.



Answered By - Keith Thompson