Cron – Differences Between /etc/crontab, /etc/cron.d/, and /var/spool/cron/crontabs/root

cron

What is the difference between the purposes of

  • /etc/crontab
  • files under /etc/cron.d/
  • /var/spool/cron/crontabs/root

If you want to add a cron job, into which file would you add it?

manpage of cron(8) Says

/etc/cron.d/: directory that contains system cronjobs stored for
different users
.

What does "stored for different users" mean?
It looks confusing with files under /var/spool/cron/crontabs/.

https://unix.stackexchange.com/a/478581/674 quotes from cron manpage:

In general, the system administrator should not use /etc/cron.d/, but use the standard system crontab /etc/crontab.

Shall a sysadmin add a job to /etc/crontab, /etc/cron.d/ or /var/spool/cron/crontabs/root?

Thanks.

Best Answer

/etc/crontab is the historical location for "system" jobs. It's not necessarily used on all systems (eg RedHat 7 and derivatives has an "empty" entry), or it may have commands to call out to cron.daily and others.

/etc/cron.d/* is, essentially, the same as /etc/crontab but split out into separate files. This makes it easy for packages to add new cron entries; just put them in this directory.

So, for example, on CentOS 7:

% rpm -ql sysstat | grep cron
/etc/cron.d/sysstat

% sudo cat /etc/cron.d/sysstat
# Run system activity accounting tool every 10 minutes
*/10 * * * * root /usr/lib64/sa/sa1 1 1
# 0 * * * * root /usr/lib64/sa/sa1 600 6 &
# Generate a daily summary of process accounting at 23:53
53 23 * * * root /usr/lib64/sa/sa2 -A

You can see these entries match what would otherwise be in /etc/crontab.

Before the cron.d was designed, a script would need to edit /etc/crontab to do the same work, which is more hard work, and likely to go wrong.

/var/spool/cron/crontabs is the stuff managed by the crontab command.

So...

If a cron job is to be deployed by a package then the package should put the file into /etc/cron.d/. Enterprise automation tools may also do that.

If a sysadmin (or any other user) wants to add a cron job with crontab -e then it will be put into /var/spool/cron/crontabs/

Related Question