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
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
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.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# 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.
/etc/crontab”, as quoted above. To be absolutely clear,/etc/crontabdoesn’t include files from/etc/cron.dby default. (As an administrator, you could place commands in/etc/cron.dand use them in/etc/crontab, but that would be thoroughly confusing.) – Stephen Kitt Jul 26 '18 at 23:59/var/spool/cron/crontabs/rootor/etc/crontab? – Tim Oct 31 '18 at 18:55/var/spool/cronfor user-specific jobs. Any system job should go in/etc/crontabor/etc/cron.{hourly,daily,weekly,monthly}IMO. – Stephen Kitt Nov 01 '18 at 14:32/var/spool/cron/crontabs/rootfor system jobs? root-specific jobs are system jobs, I guess? (3)anacronby default only runs/etc/cron.{hourly,daily,weekly,monthly}/*, so isanacronused only for system jobs not for user-specific jobs? – Tim Nov 01 '18 at 14:57/var/spool/cron/crontabsis for user-specific jobs. If an admin chooses to use root as a user account, then it would make sense for root’s jobs (the user’s, not the system’s) to go in/var/spool/cron/crontabs/root. But since root shouldn’t be used as a user account, I tend to think that/var/spool/cron/crontabs/rootshouldn’t be used either. – Stephen Kitt Nov 01 '18 at 15:02sudo crontab -ewill create/var/spool/cron/crontabs/root, so I thought that that file can be created so must be used for some purpose. – Tim Nov 01 '18 at 15:06crontab -erun as any user will create a user-specific crontab for that user; root is no different. But the way root should be used means there’s not much point in having a root-specific crontab in/var/spool/cron. – Stephen Kitt Nov 01 '18 at 15:12man cron). So filenames in/etc/cron.d/which match the shell glob*[!A-Za-z0-9_-]*are ignored! Hence things like*.dpkg-distorvi-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/etc/cron.d*MUST* include theuserfield or they simply will not run - with nothing being logged to indicate what the problem is. – FKEinternet Oct 28 '19 at 22:00