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:

  1. Open the terminal and input the following command to enter the Scheduled Task editing interface:
  2. Edit the cron table
  3. 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.
  4. 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:
  5. Minutes (0-59), hours (0-23), day (1-31), month (1-12), day of the week (0-7), command
  6. Minute: represents the task execution time in each hour (0-59)
  7. Hour: Represents the specific hour of each day to execute tasks (0-23)
  8. Day: represents the date of the month on which the task is to be executed (1-31)
  9. Month: Represents the number of the month in which the task is carried out each year (1-12)
  10. Days of the week are represented by numbers ranging from 0 to 7, where 0 and 7 both represent Sunday.
  11. Command: The command to be executed or the path to the script.
  12. For example, the following command indicates that a script will be executed at 9:30 every day.
  13. Run the script located at “/path/to/script.sh” every day at 9:30 AM.
  14. After finishing editing, save and exit the editor.
  15. After completing the scheduled task configuration, you can use the following command to view the list of scheduled tasks for the current user.
  16. list all scheduled tasks
  17. To delete the scheduled tasks of the current user, you can use the following command:
  18. Remove all cron jobs.

Notes to be aware of:

  1. The path to the configuration file for scheduled tasks is /var/spool/cron/crontabs/username, where username is the current user’s username.
  2. 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.
bannerAds