How do I schedule tasks in Linux using cron?
Published on Aug. 22, 2023, 12:20 p.m.
To schedule tasks in Linux using cron
, you can follow these steps:
- Open the terminal
- Type
crontab -e
and hit enter. This will open the crontab configuration file for the current user. - Use the following syntax to create a new scheduled task:
* * * * * /path/to/command arg1 arg2
The syntax consists of five time and date fields, followed by the command to be executed. The fields are separated by a space, and their values are as follows:
- Minute (0-59)
- Hour (0-23)
- Day of the month (1-31)
- Month (1-12)
- Day of the week (0-6 with 0=Sunday)
The asterisk symbol “*” means the task should be executed on every occurrence of the relevant field.
- Save and exit the file.
For example, the following command would run a script every day at 2:30am:
30 2 * * * /path/to/script.sh
You can also use special characters in the fields to create more complex schedules. For example, the following command would run a script every 15 minutes:
*/15 * * * * /path/to/script.sh
After you’ve set up your cron jobs, you can use the crontab -l
command to display a list of your scheduled tasks.