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