View all posts

How to run a cron job automatically at boot

System tasks and processes are scheduled using the Cron daemon in Linux. Scheduled jobs are stored and read using crontab tables (crontab).

Using the crontab command, you can control when and how jobs are executed. By using crontab, you can set the execution time of a job down to the minute, without having to write loops and timing logic.

By using the following command, you can view the cron task list:

crontab -l

Using the following command, you can edit the cron task list:

crontab -e

When you have multiple text editors installed, the system prompts you to pick one for updating the cron task list. Just select your preferred option using the number between brackets. Our default option will be vim.

Add a line called @reboot to the task list if you want your cron job to run at every system boot. At startup, the job specified by this string starts immediately after Linux is rebooted.

The syntax for an @reboot string is:

@reboot [path to command] [argument1] [argument2] … [argument n]
@reboot [path to shell script]

If you wish to run a job with a delay after the reboot, add the sleep command to the @reboot string:

@reboot sleep [time in seconds] && [path to job]

That’s it!

Related Articles...

How to sync server time

How to sync server time in CentOS & AlmaLinux

The NTP protocol synchronises the clocks of computers over a network. In this article, we will show you how to quickly sync the server time. Many systems-related tasks and processes require the correct timezone. For example, the cron daemon uses... Read more

This website uses cookies

We use cookies for the analysis of our visitor data, to improve our website, and to give you a great website experience. For more information about the cookies we use, please see our cookie policy.