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?

Stephen Kitt
  • 434,908

3 Answers3

58

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.

Stephen Kitt
  • 434,908
  • 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 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
  • 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. – Stephen Kitt Jan 15 '18 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 Jan 15 '18 at 22:08
  • Dear @StephenKitt I was sad to discover that bash.bashrc variables aren't expanded on /etc/cron.d/ files. Also, I seem not to be able to source files there. – Arcticooling Jan 15 '18 at 22:58
  • @Arcticooling what do you mean by “source files there”? Re bashrc, that’s been answered with one of your other questions. – Stephen Kitt Jan 16 '18 at 07:06
  • Thanks. I have a similar question and appreciate your help https://unix.stackexchange.com/questions/458713/how-are-files-under-etc-cron-d-used – Tim Jul 26 '18 at 23:48
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.

ilkkachu
  • 138,973
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