47

What is the main difference between the directory cron.d (as in /etc/cron.d/) and crontab?

As far as I understand, one could create a file like /etc/cron.d/my_non_crontab_cronjobs and put whatever one wants inside it, just as one would put them in crontab via crontab -e.

So what is the main difference between the two?

2
  • location in the system, and format. Commented Jan 15, 2018 at 19:39
  • Format? From 2 examples I've seen it also has the 5 time units syntax, or I'm confusing it with crontab. Commented Jan 15, 2018 at 19:47

3 Answers 3

59

The differences are documented in detail in the cron(8) manpage in Debian. The main difference is that /etc/cron.d is populated with separate files, whereas crontab manages one file per user; it’s thus easier to manage the contents of /etc/cron.d using scripts (for automated installation and updates), and easier to manage crontab using an editor (for end users really).

Other important differences are that not all distributions support /etc/cron.d, and that the files in /etc/cron.d have to meet a certain number of requirements (beyond being valid cron jobs): they must be owned by root, and must conform to run-parts’ naming conventions (no dots, only letters, digits, underscores, and hyphens).

If you’re considering using /etc/cron.d, it’s usually worth considering one of /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, or /etc/cron.monthly instead.

8
  • 2
    And for a real-world example, the items in cron.d are typically added via package installation, whereas the various crontab files are full of things created by a corresponding user. IE, the certbot or letsencrypt package puts an entry in cron.d - before the package was available, the sysadmin (root) would add an entry in the root user's crontab pointing to a shell script/etc. to keep the certificates up to date.
    – ivanivan
    Commented Jan 15, 2018 at 19:50
  • Thanks Stephen! Why do you recommend using hourly/weekly/monthly cron directories? And BTW, why is the man page there called cron(8) what is the 8 for (the man page for crontab named crontab(1)). Commented Jan 15, 2018 at 19:52
  • 1
    @Arcticooling, see this question for an explanation of the (8) Commented Jan 15, 2018 at 19:56
  • 1
    The hourly etc. directories make it simpler to add jobs (no need to figure out the exact syntax), and more importantly, allow the system to vary the real execution time depending on circumstances; this is particularly useful on systems which aren’t always on, with anacron. Commented Jan 15, 2018 at 21:28
  • 2
    The downside of the hourly|daily|weekly|monthly directories is that (AFAICT), they always run as root, and don't directly support running with lesser privilege.
    – ilkkachu
    Commented Jan 15, 2018 at 22:08
10

I would say the main difference is the obvious one: /etc/crontab and /etc/cron.d are not modifiable by regular users, while the per-user crontabs in /var/spool/cron that the crontab tool edits, are. (Subject to cron.allow and cron.deny, of course.) Of course on a system where you are the administrator, you could use either. On a shared system, the regular users can only use one.

Related to this is the extra field in the system-wide files, the username under which the job runs.

0

Apart from the differences pointed out in the other answers, one major disadvantage of crontab -e from the usage point of view is how easily you can accidentally delete all your crons with one simple mistake.

Command crontab -r removes, without warning or prompt, all the cron entries you would have in your crontab. And guess what, the letter e used for editing is just next to r on the keyboard!

Hence /etc/cron.d/ >> crontab -e

You must log in to answer this question.

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