Cron Expressions
The cron utility is responsible for job/task scheduling. These cron jobs can be configured to run periodically at defined intervals of time. That’s a nice and simple way to automate execution of repetitive tasks (starting/stopping services, initiating some pipeline jobs, watching disk space utilisation, automating backups).
Crontab
Cron stores it’s instructions in a file (crontab) using a simple format like what to run and when.
15 10 * * MON-FRI /home/user/scripts/service/export-snapshot.sh
Note
“At 10:15 on every day-of-week from Monday through Friday.”
Commands
- Edit crontab file: crontab -e
- List tasks: crontab -l
- Remove task: crontab -r
- Show last time crontab file was changed: crontab -v
Format
┌───────────── minute
│ ┌───────────── hour
│ │ ┌───────────── day of the month
│ │ │ ┌───────────── month
│ │ │ │ ┌───────────── day of the week
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
* * * * * <command to execute>
Fields
Field | Values | Wildcards |
---|---|---|
Minutes | 0-59 | ,-*/ |
Hours | 0-23 | ,-*/ |
Day-of-month | 1-31 | ,-*?/LW |
Month | 1-12 or JAN-DEC | ,-*/ |
Day-of-week | 1-7 or SUN-SAT | ,-*?L# |
Wildcards
Wildcard | Description | Example |
---|---|---|
* |
all values in the field | * |
, |
value list separator | 0,10,15 |
- |
range of values | MON-FRI |
/ |
step values | */15 |
W |
week day | 10W |
L |
last (last day of week, month) | 6L |
# |
N-th occurence | 6#3 |
? |
any | ? |
Minutes
At every minute:
* * * * * <command>
At every 10th minute:
*/10 * * * * <command>
At minute 0, 10, 20, 30, 40, and 50:
0,10,20,30,40,50 * * * * <command>
Hours
At minute 0 past every hour:
0 */1 * * * <command>
At 00:00:
0 0 * * * <command>
At minute 0 past every 3rd hour:
0 */3 * * * <command>
Day of month
At 10:00 on day-of-month 5 and 10:
0 10 5,10 * * <command>
Month
At every minute in April:
* * * 4 * <command>
At minute 0 past every hour in April:
0 */1 * 4 * <command>
At 09:00 on day-of-month 1 in every 2nd month:
0 9 1 */2 * <command>
Day of week
At 09:00 on every day-of-week from Monday through Friday:
0 9 * * 1-5 <command>
0 9 * * MON-FRI <command>