When should we restart crontab in Linux?
This is the official statement from man page of cron
cron checks each minute to see if its spool directory's
modification time (or the modification time on /etc/crontab) has changed,
and if it has, cron will then examine the modification time on all
crontabs and reload those which have changed. Thus cron need not be
restarted whenever a crontab file is modified.
So it is not required to restart crond service every time you add a new job to crontab. You can just use crontab -l
to list the available jobs.
Although it is possible that if you are remotely modifying /var/spool/cron/crontabs/root
then a crond service restart is required.
cron.service
while on RHEL/Fedora/CentOS/AlmaLinux/RockyLinux/Fedora servers you will have crond.service
.
Under what conditions should we restart crontab?
Restarting the cron daemon (crond) is generally an uncommon operation because the cron daemon automatically detects changes to individual user crontabs (edited using crontab -e
) and system crontab files within specific directories (/etc/cron.d/
, /etc/cron.hourly/
, etc.). However, there are specific scenarios where you might consider restarting the cron service:
1. Modification of System-Wide Crontab Files:
When you make direct edits to system-wide crontab configurations like the /etc/crontab
file or files within /etc/cron.d/
, a restart might be necessary to ensure that the cron daemon picks up the changes.
2. Changes in Cron Configuration:
If there are modifications in the cron daemon’s configuration files, you might need to restart the service. These configuration adjustments could be related to environment variables or the path to executables that cron uses.
3. Debugging:
For debugging purposes, restarting the cron service can be useful. If a cron job is not running as expected, restarting the cron service might resolve issues related to the daemon not correctly recognizing the scheduled jobs.
4. Updating Cron Daemon:
If there’s an update or modification to the cron daemon itself, a restart will be necessary to apply the changes.
Check cron/crond service status
Users expect the operating systems they use to schedule some tasks and run them when the time comes. On Linux operating systems, cron is used to create scheduled tasks. Each scheduled task is written to the crontab file (a separate crontab file is available for each user). For more information about cron, you can review this article.
These written cron tasks are actually automatically checked by the cron service every 1 minute.
foc@pardus:~$ sudo systemctl status cron.service
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2022-09-07 08:36:20 +03; 53min ago
Docs: man:cron(8)
Main PID: 432 (cron)
Tasks: 1 (limit: 2319)
Memory: 832.0K
CGroup: /system.slice/cron.service
└─432 /usr/sbin/cron -f
Sep 07 08:36:20 pardus systemd[1]: Started Regular background program processing daemon.
Sep 07 08:36:21 pardus cron[432]: (CRON) INFO (pidfile fd = 3)
Sep 07 08:36:21 pardus cron[432]: (CRON) INFO (Running @reboot jobs)
Sep 07 09:17:01 pardus CRON[1340]: pam_unix(cron:session): session opened for user root by (uid=0)
Sep 07 09:17:01 pardus CRON[1341]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Sep 07 09:17:01 pardus CRON[1340]: pam_unix(cron:session): session closed for user root
How to Restart Crontab?
It is sufficient to trigger the cron service to reread the crontab file. A problem with the cron/crond service causes the crontab not to be read and not to be used in general. These are some of the commands to restart crond service, you can check them based on your distribution such as Debian or Red Hat based:
On Debian/Ubuntu/Mint based Linux servers:
$ sudo service cron restart
Or:
$ sudo systemctl restart cron
Or:
$ sudo /etc/init.d/cron restart
On RHEL/Fedora/CentOS/AlmaLinux/RockyLinux/Fedora servers:
$ sudo systemctl restart crond
Or (This is deprecated):
$ sudo service crond restart
How to Reload Crontab?
However, restarting the service may cause the active cron tasks to stop. So a better method might be to reload the service:
On Debian/Ubuntu/Mint based Linux servers:
$ sudo service cron reload
Or:
$ sudo systemctl reload cron
Or:
$ sudo /etc/init.d/cron reload
On RHEL/Fedora/CentOS/AlmaLinux/RockyLinux/Fedora servers:
$ sudo systemctl reload crond
Or (This is deprecated):
$ sudo service crond restart
Summary
Restarting the cron service is seldom required and generally reserved for system-wide changes or troubleshooting. Understanding when it's necessary to restart the cron daemon is essential to maintain the effective and reliable execution of scheduled cron jobs.
For system administrators, cron tasks are valuable. In this article, we tried to share useful commands to restart crontab. For more information about the cron service, you can visit the man page.