cron syntax and examples on Linux
On linux and other nix systems such as mac, the command below will open up crontab file in a text edit, and we can add tasks in this file to schedule them to run. This crontab file is specific for the user who opens up and adds tasks to it, the tasks will be ran as this user. In step 2, we are creating a cron schedule file which will work for any user, and any user can add tasks to it as long as the user has root access.
1. Linux Crontab format
Minute Hour DayOfMonth Month DayOfWeek Command
2. Setting up cron task on linux
Create a file /etc/cron.d/mycron with the line below, it will run the script /opt/scripts/backup.sh every minutes.
* * * * * /opt/scripts/backup.sh
*:Every mintue
*:Every hour
*:Every day of the month
*:Every month
*:Every day of the week
3. Schedule a job to run on a specific time.
For example, the line below schedules the script /opt/scripts/backup.sh on August 15, 11:10 PM
10 23 15 08 * /opt/scripts/backup.sh
10: 10th mintue
23: 11PM
15: 15th day of the month
08: August
*: Every day of the week
4. Schedule a job to run more than once every day.
For example, the line below schedules the script /opt/scripts/backup.sh to run at 1AM and 1PM everyday.
00 01,13 * * * /opt/scripts/backup.sh
00: 0th minute
1,13: 1AM and 1PM
*: Every day of the month
*: Every month
*: Every day of the week
5. Schedule a job to run during a range of time.
For example, the line below schedules the script /opt/scripts/backup.sh to run once every hour from 9am to 9pm every day.
00 09-21 * * * /opt/scripts/backup.sh
00: 0th mintue
09-21: 9am to 9pm
*:Every day of the month
*:Every month
*:Every day of the week
6. Schedule a job to run every hour on weekends.
00 * * * 6-7 /opt/scripts/backup.sh
00: 0th minute
*: Every hour
*: Every day of the month
*: Every month
*: Saturday and Sunday
7. Schedule a job to run every 5 minutes.
*/5 * * * * /opt/scripts/backup.sh
*/5: Every 5 minutes
*: Every hour
*: Every day of the month
*: Every month
*: Every day of the week
Search within Codexpedia
Search the entire web