How to Run Cron Job When System Reboot in Linux
In this tutorial we are going to learn how to schedule cron jobs to run everytime after system reboot in Linux.
The Linux crontab has a time option called @reboot which allows to run a cron job once after reboot every time.
In the schedule task, Replace the 5 initial time and date fields with @reboot.
#Example /etc/crontab @reboot username command
#example crontab -e @reboot command
The @reboot method works both System cron jobs(/etc/crontab) and user's crontab (crontab -e).
Example - Run systemwide cron job when system reboot
Following is an example for the /etc/crontab file in Linux.
@reboot root /usr/local/bin/script.sh
As per the above example, the command /usr/local/bin/script.sh will run after the system restart.
Example - User crontab
You can open your crontab file using the crontab -e command.
@reboot date >> ~/date.txt
As per the above example, The system date will be written to the date.txt file when the Linux system restart.
The @reboot method will helpful in a situation where you want to start a script or service automatically once the Linux system reboot.