Linux: Run Cron Job on Reboot

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.

@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.

$ 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.

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.

$ 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.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }