Objective: Run or execute a cron job after a reboot or system startup.
cron
is a daemon to execute scheduled tasks on Unix and Unix-like operating systems. The Vixie cron
implementation, which is included in many Linux distributions supports additional nonstandard scheduling definitions. One of them is the @reboot
entry.
To run a cron job after every reboot on Vixie cron
, add the following crontab
entry. You can modify your crontab
by running crontab -e
.
1 |
@reboot /path/to/cron/job |
If the above crontab entry does not get executed after a system reboot, most probably you are using another implmentation of cron
and not using Vixie cron
.
There is no standard way of knowing if you are running Vixie cron
. Below is how I usually determine if Vixie cron
is being used.
1 2 |
$ strings $(which cron) | grep -i copyright @(#) Copyright 1988,1989,1990,1993,1994 by Paul Vixie |
The other method is to run man cron
and check if Paul Vixie is stated as the author of the cron implementation.
You can refer to man -s 5 cron
for the list of special strings supported by Vixie cron
. Below is an extract from the man page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Instead of the first five fields, one of eight special strings may appear: string meaning ------ ------- @reboot Run once, at startup. @yearly Run once a year, "0 0 1 1 *". @annually (same as @yearly) @monthly Run once a month, "0 0 1 * *". @weekly Run once a week, "0 0 * * 0". @daily Run once a day, "0 0 * * *". @midnight (same as @daily) @hourly Run once an hour, "0 * * * *". Please note that startup, as far as @reboot is concerned, is the time when the cron(8) daemon startup. In particular, it may be before some system daemons, or other facilities, were startup. This is due to the boot order sequence of the machine. |
If the system is not rebooted and if the cron daemon process gets restarted, the cron daemon is smart enough not to run the @reboot
jobs. I did a simple test (on Ubuntu 16.04 LTS) by restarting the cron daemon and below is the output from the syslog
file.
1 2 3 |
$ sudo tail syslog | grep cron Aug 27 16:51:13 anfield cron[12588]: (CRON) INFO (pidfile fd = 3) Aug 27 16:51:13 anfield cron[12588]: (CRON) INFO (Skipping @reboot jobs -- not system startup) |
Vixie cron
is available on both Linux and BSD systems.