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 |
0 9 * * 1-5: Friday 9 October is included. Saturday (6) and Sunday (0) aren't in1-5, so both are skipped, and the next run is on Monday.*/15 * * * *: 14:37 is already past 14:30, so the next one is 14:45.0 0 1 * *: only the 1st. 1 October has passed, so the next is 1 November.
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
crontab -eopens an editor. You add a line, save, then quit the editor, and crontab installs the schedule. The end of that line,>> /home/budi/report.log 2>&1, saves the output to a log file (see the ⚠️ section below).- If a field is wrong, for example minute
60(minutes only go from 0 to 59), crontab refuses to save. The messages arebad minuteanderrors in crontab file, can't install.The Lab shows the same messages. - There are shorthands too:
@hourly=0 * * * *,@daily=0 0 * * *,@weekly=0 0 * * 0,@monthly=0 0 1 * *,@yearly=0 0 1 1 *, and@reboot= once every time the server starts.
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
split(" ")cuts the text at every space into an array (a list of values) of five strings.split(/\s+/)is safer when there is more than one space between two fields.const [minute, hour, day, month, weekday] = ...names the array items in the order of the fields. They all stay strings, soweekdayholds"1-5"as it is.padStart(2, "0")adds zeros in front until the length is 2:"9"becomes"09","0"becomes"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
- Cron doesn't read your
PATH. Cron runs commands with very few environment variables, usually justPATH=/usr/bin:/bin. So a command that works in your terminal can fail in cron, because its program is not found in those folders. Write the full path:/usr/bin/node /home/budi/backup.js. - The output disappears. Cron has no screen to show the output on. Save the output to a log file with
>> /home/budi/backup.log 2>&1. The2>&1part makes error messages go to the same file too. - Thinking the date and the weekday are joined with "and". When both are filled in, either one matching is enough.
- Forgetting the time zone. Cron uses the server's clock. Many servers run on UTC (the world's standard time, 8 hours behind Singapore), so
0 9 * * *on a UTC server runs at 17:00 in Singapore.
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.