Linux Commands

How To Execute crontab at System Boot Time

Are you looking for a solution to running crontab every time your server or system reboots? This is possible by using the @reboot option. The default way of running cron jobs is specifying their date and time, followed by the path to the command to execute. The same concept gets used when defining a command that should execute whenever the server restarts. The only difference is that, in this case, we don’t specify the date and time. Instead, we use the @reboot followed by the path to the command. Let’s see how that works.

How To Schedule crontab To Execute at System Boot Time

Linux and Unix systems come preinstalled with the cron utility, a job scheduler that makes it easy to schedule jobs on the crontab file. The same utility is available for macOS. The syntax for scheduling jobs at given intervals is provided below:

Min Hour Day-of-month Month Day-of-week [command]

For instance, you could use the following command if you need to run a backup script every 20 minutes from 3:00 p.m. to 4:00 p.m. daily.

*/20 15-16 * * * /path/to/backup.sh

The previous command works when you need to execute a job at a particular time, but what if you need the same command to run after every boot?

To set the same command to run when the system reboots, replace the date and time fields with @reboot. The new command would be:

@reboot /path/to/backup.sh

In the previous command, the @reboot specifies that cron should execute the specified command after every boot. Again, we must add the command to the crontab file.

Use the following command to open the crontab file and edit it to add our new job.

crontab -e

Note that we are creating the job for the current user. If you need to schedule the task for a different user, for instance, a user named linuxhint1, the following command would be:

crontab -e -u linuxhint1

Once the crontab file opens, add the command at the bottom line. Save the file and exit.

Also, note that we are using a nano editor, your editor may be different, but the command is the same.

If the job is successfully scheduled, you should get a message like the one shown in the following image to indicate a successful installation of the new crontab:

Still, you can use the following command to list the scheduled jobs.

crontab -l

If you no longer wish to execute the command at boot time, you can remove it by editing the crontab file or using the command provided below. Note that the following command deletes all scheduled jobs. If that’s not what you wish to achieve, scroll down on the crontab and manually delete the job using an editor.

crontab -r

The @reboot executes a command immediately after boot time. However, you can specify a sleep period before the command runs. For instance, if you need the command to execute 10 minutes after boot, you must set the time in seconds.

The command would be.

@reboot sleep 600  /path/to/backup.sh

600 represents the 10 minutes expressed in seconds, and sleep is the option to use when specifying the time before execution.

Our new crontab file would be set, as shown below:

The next time you reboot your server, the backup script or the set command will execute after 10 minutes.

Lastly, we can verify that the scheduled job will run by checking the status for crond service. It should be active. Use the following command to check its status:

sudo systemctl status cron.service

You are good to go if you get an output like the following output:

If the status of crond is not active, you can enable it using the following command, then check the status:

sudo systemctl enable cron.service

That’s it. Your command will execute at boot time.

Conclusion

Knowing how to schedule jobs at boot time is essential for Linux administrators. Luckily, this guide covered a comprehensive hands-on guide on how to go about that using the Linux cron utility. In addition, we discussed how you could set a sleep time before the command executes.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.