Cron Jobs & Scheduling — Terminal

Cron runs commands automatically on the schedule in your crontab, the list of schedules each user has. Each line holds five time fields (minute, hour, date…

Back up the database at 2 a.m., send a report every Monday, clear temporary files every hour. You don't want to stay awake at night just to type those commands. Linux and macOS have cron, a service (a program that keeps running in the background) that runs commands automatically on a schedule. You write the schedule once, and cron runs the command every time the clock matches, even while you're asleep or logged out. Cron is like a repeating alarm on your phone, for example "Monday to Friday, 06:00". You don't switch that alarm on every morning: you set the pattern once. In cron, the alarm's pattern is written as five fields, and instead of a ringing sound, a command runs.

One crontab line: five time fields, then a command

Cron schedules are kept in a crontab (cron table), one list per user. Each line holds five time fields, then the command to run:

┌───────── minute   (0-59)
│ ┌─────── hour     (0-23)
│ │ ┌───── date     (1-31)
│ │ │ ┌─── month    (1-12)
│ │ │ │ ┌─ weekday  (0-7: 0 and 7 = Sunday, 1 = Monday, 6 = Saturday)
│ │ │ │ │
0 9 * * 1-5  /home/budi/report.sh

Every minute, cron checks each line: does the time right now match all five fields? If it does, the command runs. The line above means minute 0, hour 9, any date, any month, Monday to Friday: every weekday at 09:00.

Plain cron has no seconds field. So, at most, a line runs once a minute.

The symbols in each field

Written Meaning Example
* every value in the minute field: every minute
30 one value in the minute field: minute 30
0,30 a list of values in the minute field: minutes 0 and 30
1-5 a range, both ends included in the weekday field: Monday to Friday
*/15 a step of 15, counted from the start of the range in the minute field: 0, 15, 30, 45
9-17/2 a range with a step in the hour field: 9, 11, 13, 15, 17

*/15 means "minutes divisible by 15", not "15 minutes after the last run". The count starts again every hour. That's why */40 runs at minutes 0 and 40: a gap of 40 minutes, then 20 (from minute 40 to minute 0 of the next hour).

Reading a schedule: five examples from the Lab

The Lab's clock is stopped at Thursday, 8 October 2026, 14:37. The row of five time fields is called a cron expression. This table shows the next three runs of each expression, counted from the Lab's clock:

Expression Meaning Next three runs
0 9 * * 1-5 Every weekday at 09:00 Fri 9 Oct, Mon 12 Oct, Tue 13 Oct, at 09:00
*/15 * * * * Every 15 minutes 14:45, 15:00, 15:15
0 0 * * * Every day at 00:00 Fri 9 Oct, Sat 10 Oct, Sun 11 Oct, at 00:00
0 0 1 * * Start of every month at 00:00 1 Nov, 1 Dec, 1 Jan 2027, at 00:00
30 2 * * 3 Every Wednesday at 02:30 Wed 14 Oct, Wed 21 Oct, Wed 28 Oct, at 02:30

Both the date and the weekday filled in

Guess first: when does the schedule 0 0 13 * 5 (00:00, the 13th, weekday 5 = Friday) run? Only on a Friday that falls on the 13th, or more often than that?

The answer: more often. When the date field and the weekday field are both restricted (neither starts with *), cron runs the command when either one matches.

So 0 0 13 * 5 runs on every 13th and also every Friday. From Thursday 8 October 2026, the next runs are Fri 9 Oct, Tue 13 Oct, Fri 16 Oct, Fri 23 Oct. Not just "Friday the 13th".

When one of those two fields starts with *, that field doesn't take part. 0 9 * * 1-5 only looks at the weekday, and 0 0 1 * * only looks at the date.

Managing your crontab

Three commands manage your crontab:

crontab -e   # open your crontab in an editor; save to install it
crontab -l   # print your crontab
crontab -r   # delete ALL your schedules, without asking

This is what happens when Budi installs his first schedule:

$ crontab -l
no crontab for budi
$ crontab -e
crontab: installing new crontab
$ crontab -l
0 9 * * 1-5 /home/budi/report.sh >> /home/budi/report.log 2>&1

These are the schedulers you can choose:

Option When to pick it
cron The default in this lesson, because it is on almost every Linux server and needs only one line per schedule. But if the computer is off or asleep at that time, the command is skipped, and it runs again only at the next scheduled time.
systemd timer A Linux server that is sometimes off. systemd manages services on most Linux systems (the systemd lesson covers it), and a timer is the systemd scheduler. With the setting Persistent=true, a missed run starts as soon as the machine is on again.
launchd (macOS), Task Scheduler (Windows) Schedules on your own laptop. launchd manages services on macOS, and Task Scheduler is the scheduler that comes with Windows. Apple calls cron deprecated (no longer recommended) and recommends launchd. If the Mac is asleep at that time, launchd runs the job when the Mac wakes up.
GitHub Actions (schedule), Cloudflare Cron Triggers Tasks that don't need your computer, for example calling a URL every hour. GitHub Actions and Cloudflare Cron Triggers are online services: the schedule runs on GitHub's or Cloudflare's servers, in UTC time (see the ⚠️ part below).

Splitting a cron expression with JavaScript

The exercise is written in JavaScript. You split an expression into five strings, then describe it. For example:

const [minute, hour, day, month, weekday] = "0 9 * * 1-5".split(" ");

console.log(minute);  // 0
console.log(weekday); // 1-5
console.log(String(hour).padStart(2, "0") + ":" + String(minute).padStart(2, "0")); // 09:00

The exercise's descriptions cover only the four expressions at the top of the five-example table: Every weekday at 09:00, Every 15 minutes, Every day at 00:00 and Start of every month at 00:00. Any other expression, such as 30 2 * * 3, is described as it is: Scheduled: 30 2 * * 3.

⚠️ Why the job doesn't run, or runs at an odd hour

TL;DR: Cron runs commands automatically on the schedule in your crontab, the list of schedules each user has. Each line holds five time fields (minute, hour, date, month, weekday; 0 and 7 = Sunday), then the command. * is every value, , separates a list of values, - makes a range, and */15 is a step counted from the start of the range (in the minute field: every 15 minutes). When the date and the weekday are both filled in (neither starts with *), cron runs when either matches. crontab -e adds schedules, -l shows them, and -r deletes them all. Write the full path of each program, because cron's PATH holds only a few folders. Send output to a log with >> file.log 2>&1, and remember that cron uses the server's time zone.

⚡ Five time fields, then a command

Write:

0 9 * * 1-5 /home/budi/report.sh

Meaning: This is one schedule line for cron, the service that runs commands automatically on a schedule. The fields go minute, hour, date, month, weekday, then the command. This line means minute 0, hour 9, any date and any month (*), weekdays 1 to 5 (Monday to Friday): every working day at 09:00.

⚡ The symbols in each field

Meaning: * means every value, 0,30 a list of values, 1-5 a range, and */15 a step. In the minute field, */15 means minutes 0, 15, 30 and 45.

⚡ Both the date and the weekday filled in

Guess first: 0 0 13 * 5 means 00:00, the 13th, weekday 5 (Friday). When does it run? Only on Friday the 13th?

More often. When the date and the weekday are both filled in (neither starts with *), cron runs when either one matches: every 13th, and also every Friday.

Meaning: When one of those two fields (date or weekday) starts with *, that field doesn't take part. Only the other field counts.

⚡ Add a schedule

Write:

crontab -e
crontab -l

Meaning: crontab -e opens your crontab (your list of cron schedules) in an editor to add a schedule, and crontab -l shows what is in it. Careful with crontab -r: that command deletes all your schedules without asking.

What you will learn