How to Set Up Linux Cron Jobs
In Linux systems, you can use the crontab command to configure scheduled tasks. The specific steps are as follows:
- Open the terminal and input the following command to enter the Scheduled Task editing interface:
- Edit the cron table
- If it is the first time editing a scheduled task, the system will require you to choose an editor. You can choose your preferred editor, such as selecting “1” to use the vi editor.
- After entering the editing interface, you can add, modify, or delete scheduled tasks. Each line represents a scheduled task, and the format for each line is:
- Minutes (0-59), hours (0-23), day (1-31), month (1-12), day of the week (0-7), command
- Minute: represents the task execution time in each hour (0-59)
- Hour: Represents the specific hour of each day to execute tasks (0-23)
- Day: represents the date of the month on which the task is to be executed (1-31)
- Month: Represents the number of the month in which the task is carried out each year (1-12)
- Days of the week are represented by numbers ranging from 0 to 7, where 0 and 7 both represent Sunday.
- Command: The command to be executed or the path to the script.
- For example, the following command indicates that a script will be executed at 9:30 every day.
- Run the script located at “/path/to/script.sh” every day at 9:30 AM.
- After finishing editing, save and exit the editor.
- After completing the scheduled task configuration, you can use the following command to view the list of scheduled tasks for the current user.
- list all scheduled tasks
- To delete the scheduled tasks of the current user, you can use the following command:
- Remove all cron jobs.
Notes to be aware of:
- The path to the configuration file for scheduled tasks is /var/spool/cron/crontabs/username, where username is the current user’s username.
- The cron service is enabled by default; if it is not, you can start the cron service using the following command: sudo service cron start.