6

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.

9
  • 1
    Related: How are files under /etc/cron.d used?. Oct 31, 2018 at 8:37
  • @StephenKitt Thanks. Does "/etc/cron.d/: directory that contains system cronjobs stored for different users" mean "/etc/cron.d/: directory that contains system cronjobs stored for different packages" instead?
    – Tim
    Oct 31, 2018 at 17:35
  • What version of the manpage does that come from? I don’t see it in the manpages I’ve found... Nov 1, 2018 at 14:29
  • @StephenKitt man7.org/linux/man-pages/man8/cron.8.html
    – Tim
    Nov 1, 2018 at 14:55
  • Are you using Fedora or RHEL? Nov 1, 2018 at 14:57

1 Answer 1

7

/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/

4
  • /etc/cron.d/ is only to control the behavior of cron, but not for cron jobs.
    – schily
    Oct 31, 2018 at 10:04
  • Thanks. (1) Who will add a job to /etc/crontab at when? (2) Is /var/spool/cron/crontabs/root is more similar to other users' crontab files under the same directory than to /etc/crontab or files under /etc/cron.d/, in that If a sysadmin wants to add a cron job with crontab -e then it will be put into /var/spool/cron/crontabs/root? (3) Does "/etc/cron.d/: directory that contains system cronjobs stored for different users" mean "/etc/cron.d/: directory that contains system cronjobs stored for different packages" instead?
    – Tim
    Oct 31, 2018 at 17:52
  • (4) How would you edit /etc/crontab and files under /etc/cron.d/? unix.stackexchange.com/questions/478777/…
    – Tim
    Oct 31, 2018 at 17:55
  • (5) 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?
    – Tim
    Oct 31, 2018 at 18:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .