Scheduling with cron
Run scripts automatically.
Remember the dated backup? It's only useful if it runs every day, even when you forget. Cron is the Linux scheduler. You tell it when, and what, and it runs the command for you.

- Backups must run every day
- Even when you forget
- cron runs it for you
The five time fields
A cron line starts with five time fields. Minute, hour, day of the month, month, and day of the week. A star means every. So zero, two, star, star, star means at minute zero of hour two, every day. Two AM, every night.
| Field | Values | Example |
|---|---|---|
| minute | 0 - 59 | 0 |
| hour | 0 - 23 | 2 |
| day of month | 1 - 31 | * |
| month | 1 - 12 | * |
| day of week | 0 - 6, Sunday = 0 | * |
Adding a cron job
Crontab dash L lists your scheduled jobs. There are none yet. Here we add one line. Every night at two, run the backup script. Then crontab dash L shows our new job. Cron checks this list every minute.
crontab -l no crontab for sam echo "0 2 * * * /home/sam/backup.sh" | crontab - crontab -l 0 2 * * * /home/sam/backup.sh
More schedules
A few more patterns. Star slash fifteen in the minute field means every fifteen minutes. One to five in the last field means Monday to Friday. And at reboot runs a command each time the computer starts. Normally you'd edit the list with crontab dash E, which opens an editor.
| key | value |
|---|---|
| */15 * * * * | every 15 minutes |
| 0 9 * * 1-5 | 9 am, weekdays |
| 0 2 * * * | 2 am, every day |
| crontab -e | edit your jobs |
Summary
Cron runs commands on a schedule. Five time fields, then the command. Crontab dash L lists jobs, and crontab dash E edits them. Next, our first project, a photo renamer.
Key takeaways- minute hour day month weekday
- crontab -l
- crontab -e