Introduction
The Cron daemon is a Linux utility used for scheduling system tasks and processes. It uses cron tables (crontab) to store and read scheduled jobs.
This tutorial will cover how you can use crontab to schedule jobs to be run at system reboot.
Prerequisites
- A system running Linux
- Access to a command line/terminal window
- A user account with root or sudo privileges
Crontab Command Overview
With the crontab
command, you have full control of when and how jobs are executed. Use crontab
to set job execution time down to the minute, without the need for looping and timing logic in the task.
crontab
has low resource requirements since it doesn’t reserve system memory when it isn’t running.
Crontab on Boot: Run a Cron Job at Boot Time
Open the cron task list by using the following command:
crontab -e
If you have multiple text editors installed, the system prompts you to select an editor to update the cron task list with. Use the number in the brackets to choose your preferred option. We will be using the default option, Nano.
Note: Leaving the field blank and pressing enter chooses the first option available.
To run a cron job at every system boot, add a string called @reboot
to the end of the task list. The job defined by this string runs at startup, immediately after Linux reboots.
Use the following syntax when adding a @reboot
string:
@reboot [path to command] [argument1] [argument2] … [argument n]
@reboot [part to shell script]
Note: Always use the full path to the job, script, or command you want to run, starting from the root.
Press Control + X
to exit Nano, then Y
and Enter
to save any changes you made.
For example, if we wanted to have the system date written in a file called date.txt when Linux restarts, we would add the following string:
@reboot date >> ~/date.txt
If we wanted to run the backup shell at reboot, we would add:
@reboot /root/backup.sh
Note: In some cases, the crond service needs to be enabled on boot for the configuration to function.
To check if the crond service is enabled, use:
sudo systemctl status cron.service
To enable this service, use:
sudo systemctl enable cron.service
Run a Cron Job at Boot With Delay
To run a job with a delay after the system reboots, use the sleep command when adding the @reboot
string:
@reboot sleep [time in seconds] && [path to job]
If you want to create a text file with the system date five minutes after reboot, add:
@reboot sleep 300 && date >> ~/date.txt
Remove a Reboot Command
Each @reboot
string you add to the cron task list runs a job every time Linux restarts. If you no longer wish to run a job, remove it from the task list.
To do this, open the task list using the crontab -e
command. Scroll down to the bottom to review the jobs you added.
To remove a task from the list, delete the appropriate line from the appropriate string. Press Control + X
to exit Nano, then Y
and Enter
to save changes.
Note: Learn more about the Linux at command, the alternative for cron job for scheduling jobs.
Conclusion
After following this tutorial, you understand how to use crontab to schedule jobs to run at system reboot.
For more ways to schedule jobs in crontab, check out our guide to setting up cron jobs.
Đăng ký liền tay Nhận Ngay Bài Mới
Subscribe ngay
Cám ơn bạn đã đăng ký !
Lỗi đăng ký !
Add Comment