How are files under /etc/cron.d used?

From https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

cron reads the files in /etc/cron.d/ directory. Usually system daemon such as sa-update or sysstat places their cronjob here. As a root user or superuser you can use following directories to configure cron jobs. You can directly drop your scripts here. The run-parts command run scripts or programs in a directory via /etc/crontab file:

/etc/cron.d/ Put all scripts here and call them from /etc/crontab file.

On Lubuntu 18.04, the files under /etc/cron.d seem to be crontab files not shell scripts (which was mentioned in the above link):

$ cat /etc/cron.d/anacron 
# /etc/cron.d/anacron: crontab entries for the anacron package


30 7    * * *   root    [ -x /etc/init.d/anacron ] && if [ ! -d /run/systemd/system ]; then /usr/sbin/invoke-rc.d anacron start >/dev/null; fi

My /etc/crontab file never refers to files under/etc/cron.d, contrary to what the link says:

$ cat /etc/crontab 
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.


# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

Could you explain how the files under /etc/cron.d are used? Thanks.


In Debian derivatives, including Lubuntu, the files in /etc/cron.d are effectively /etc/crontab snippets, with the same format. Quoting the cron manpage:

Additionally, in Debian, cron reads the files in the /etc/cron.d directory. cron treats the files in /etc/cron.d as in the same way as the /etc/crontab file (they follow the special format of that file, i.e. they include the user field). However, they are independent of /etc/crontab: they do not, for example, inherit environment variable settings from it. This change is specific to Debian see the note under DEBIAN SPECIFIC below.

Like /etc/crontab, the files in the /etc/cron.d directory are monitored for changes. In general, the system administrator should not use /etc/cron.d/, but use the standard system crontab /etc/crontab.

The Debian-specific section hints at the reason system administrators shouldn’t use /etc/cron.d:

Support for /etc/cron.d (drop-in dir for package crontabs)

It’s designed to allow packages to install crontab snippets without having to modify /etc/crontab.

  • Thanks. Are the files in /etc/cron.d and /etc/crontab independent of each other, and read by crond independently and separately? Or does /etc/crontab ever include the files in /etc/cron.d? – Tim Jul 26 '18 at 23:53
  • 1
    “However, they are independent of /etc/crontab”, as quoted above. To be absolutely clear, /etc/crontab doesn’t include files from /etc/cron.d by default. (As an administrator, you could place commands in /etc/cron.d and use them in /etc/crontab, but that would be thoroughly confusing.) – Stephen Kitt Jul 26 '18 at 23:59
  • Thanks. "In general, the system administrator should not use /etc/cron.d/, but use the standard system crontab /etc/crontab." When should a sysadmin use /var/spool/cron/crontabs/root or /etc/crontab? – Tim Oct 31 '18 at 18:55
  • I only ever use /var/spool/cron for user-specific jobs. Any system job should go in /etc/crontab or /etc/cron.{hourly,daily,weekly,monthly} IMO. – Stephen Kitt Nov 1 '18 at 14:32
  • 3
    People might miss, that "the file names must conform to the filename requirements of run-parts" (see Debian's man cron). So filenames in /etc/cron.d/ which match the shell glob *[!A-Za-z0-9_-]* are ignored! Hence things like *.dpkg-dist or vi-backups *~ do no harm, but if you accidentally create /etc/cron.d/very_important.crontab, this will get ignored due to the . in it! – Tino Jan 16 '19 at 19:06

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.