AI Hints
Home›Linux Tutorial›cron and First Projects
Linux · Lesson 109 of 120

Scheduling with cron

Run scripts automatically.

⏱ 2 min read▶ Video at 2:25:44🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

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.

Scheduling with cron
From the video: Scheduling with cron
  • 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.

FieldValuesExample
minute0 - 590
hour0 - 232
day of month1 - 31*
month1 - 12*
day of week0 - 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.

Terminal
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.

keyvalue
*/15 * * * *every 15 minutes
0 9 * * 1-59 am, weekdays
0 2 * * *2 am, every day
crontab -eedit 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
Next up: Project: Photo Renamer — tidy names, automatically