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.

Tim
  • 101,790
  • 1
  • @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 '18 at 17:35
  • What version of the manpage does that come from? I don’t see it in the manpages I’ve found... – Stephen Kitt Nov 01 '18 at 14:29
  • @StephenKitt http://man7.org/linux/man-pages/man8/cron.8.html – Tim Nov 01 '18 at 14:55
  • Are you using Fedora or RHEL? – Stephen Kitt Nov 01 '18 at 14:57
  • @Stephen I am using Ubuntu – Tim Nov 01 '18 at 15:02
  • OK, reading cron manpages for other systems is only likely to confuse you — there are significant variations between the different cron implementations used in various distributions. The manpage you’re referring to is the manpage for the version of cron used in Fedora, RHEL and derivatives; there, /etc/crontab is deprecated, and /etc/cron.d is how you set up cron jobs for different users (i.e., jobs which specify the user alongside the time). Debian derivatives are different. The manpage means what it says, but it doesn’t make sense if you try to apply it to Debian derivatives. – Stephen Kitt Nov 01 '18 at 15:10
  • @StephenKitt Thanks. Are user-specific crontab files under /var/spool/cron/crontabs/ also deprecated on Fedora, RHEL and derivatives? – Tim Nov 01 '18 at 15:28
  • No, they’re not deprecated — otherwise users wouldn’t be able to add their own cron jobs. – Stephen Kitt Nov 01 '18 at 15:31

1 Answers1

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/

  • /etc/cron.d/ is only to control the behavior of cron, but not for cron jobs. – schily Oct 31 '18 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 '18 at 17:52
  • (4) How would you edit /etc/crontab and files under /etc/cron.d/? https://unix.stackexchange.com/questions/478777/how-would-you-edit-etc-crontab-and-files-under-etc-cron-d – Tim Oct 31 '18 at 17:55
  • (5) 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? – Tim Oct 31 '18 at 18:33