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

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

So what is the main difference between the two?

  • location in the system, and format. – Rui F Ribeiro Jan 15 '18 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. – Arcticooling Jan 15 '18 at 19:47

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.

  • 1
    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 Jan 15 '18 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)). – Arcticooling Jan 15 '18 at 19:52
  • 1
    @Arcticooling, see this question for an explanation of the (8) – user4556274 Jan 15 '18 at 19:56
  • 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. – Stephen Kitt Jan 15 '18 at 21:28
  • 1
    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 Jan 15 '18 at 22:08

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.