$_ CLI In Depth
Learning progress 0%
lesson packages services cron

Scheduling with cron

5 min read Module 11: Packages, Services & Scheduled Jobs Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:20

What cron does

The cron daemon, usually called crond, starts at boot and stays in the background. Each minute it checks the schedule tables of every user and the system directories, finds the entries that match the current minute, and starts their commands. The commands run without a terminal, with a minimal environment, and under the account that owns the table. Cron does not wait for the command to finish, so a slow job can overlap the next run unless the job guards against it.

Because cron reads plain files, a schedule is easy to review and to copy between machines. The same file can be checked with a text editor, a diff, or a script, and nothing needs to be running in memory to keep it.

The five time fields

Every crontab line starts with five fields, separated by spaces. They are, in order, minute, hour, day of month, month, and day of week. Each field can be a number, a range such as 1-5, a list such as 1,15, a step such as */10, or an asterisk, which means every value. Minutes run from 0 to 59, hours from 0 to 23, days of month from 1 to 31, months from 1 to 12, and days of week from 0 to 7, where both 0 and 7 mean Sunday.

The command follows the five fields after a single space. Everything after the fifth field up to the end of the line is the command, so it may contain spaces and redirections. The line below runs a backup script at 02:30 every day.

BASH1 lines
30 2 * * * /usr/local/bin/backup.sh

Read it aloud as follows: at minute 30, at hour 2, on any day of the month, in any month, on any day of the week. The asterisks are the same as the word every in each field.

Steps and common schedules

A step value divides a field into regular points. The field */15 in the minute position means minutes 0, 15, 30, and 45. The field 0-30/10 means minutes 0, 10, 20, and 30. Steps are the cleanest way to write a repeated schedule, and they are easier to verify than a long list.

Some schedules are common enough to remember. Every five minutes is */5 * * * *. Every weekday at nine in the morning is 0 9 * * 1-5. The first day of each month at midnight is 0 0 1 * *. Writing a schedule and checking it against this list is a good way to catch an off-by-one mistake before it runs.

Per-user tables and the system table

Each user has a crontab, and the crontab command installs and lists that table. On Alpine the tables for busybox crond live in /etc/crontabs, one file per user, and the root table is the most important one. A user without privileges cannot read the root table, and cannot install a table for another account. The system also reads files in the directories under /etc named cron.hourly, cron.daily, and similar, whose scripts run on that schedule without a table line.

A per-user table runs as that user, so a user can schedule work for their own files without any privilege. The rest of this unit uses a file in /workspace to practice the syntax, because the lab does not run a cron daemon and does not let you install a table.

Where the output goes

Cron tries to send a command output to the owner of the table, usually by mail. Most systems do not have a working mail agent, so output is simply lost. The safe habit is to redirect both standard output and standard error to a log file that you control, and to write the log path in the crontab line itself.

BASH1 lines
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

The redirection is part of the command, so cron passes it to the shell. The double angle brackets append to the log rather than overwrite it, which keeps the history.

Note

A line in a crontab is run by a shell, but that shell has a minimal PATH. Put full paths in the command, such as /bin/tar rather than tar, so that the job works the same way from cron as it does from your terminal.

A schedule is also a promise about load. Several jobs that all start at the top of the hour compete for the disk and the CPU at the same moment, and a slow backup that starts at 02:00 can still be running when a report starts at 02:00 too. Spread the jobs across different minutes, such as 02:05 and 02:20, so that each one has room to finish. A schedule that spreads the work is also easier to read in the log, because each job has its own time window.

Cron has no built-in way to stop two runs of the same job from overlapping. A script that may run longer than its interval can protect itself with a lock file that it creates at start and removes at exit, or with the flock utility from util-linux. Adding a guard like this turns a slow job into a skipped run rather than two copies of the same work.

Hands-on: write a schedule file

Write a file with one schedule line that runs every five minutes, and check its shape with grep. The lab checks the file, so the schedule itself does not run here.

BASH2 lines
echo "*/5 * * * * /workspace/backup.sh" > mycron
cat mycron

The file holds one line that starts with the step field, followed by the path of a script. A real crontab can hold many such lines, and each one is independent of the others. The next units cover the traps that show up when you write more than one line.

Decoding a crontab line

Visualizer

A crontab line is five time fields and then a command. Decode the fields, the day-of-month and day-of-week rule, and the two pitfalls: % and the short PATH.

1 / 5
crontab line
*/15minute
9-17hour
*day of month
*month
1-5day of week
jobcommand

A line has five time fields, then the command. Read them left to right: minute, hour, day of month, month, day of week.

for t in 09:00 09:15 17:45; do date -d "today $t" '+%a %F %H:%M'; done
Terminal lab · 2 steps · real Linux in your browser

Write schedule lines to files

Write a every-five-minutes line and a weekday line, and check their time fields.

01
Write a every five minutes line
Write one crontab line that runs /workspace/backup.sh every five minutes into mycron. The first field is the step */5. ``bash echo "*/5 * * * * /workspace/backup.sh" > mycron `` Read the file back with cat mycron.
Show hint for step 1
Use five fields, with */5 in the minute field and asterisks in the other four.
02
Write a weekday line at nine
Write a line that runs /workspace/report.sh at 09:00 from Monday through Friday, into weekday.cron. Use a range in the weekday field. ``bash echo "0 9 * * 1-5 /workspace/report.sh" > weekday.cron `` The minute is 0, the hour is 9, and the weekday field is 1-5.
Show hint for step 2
The fields are minute, hour, day of month, month, and day of week. Put 1-5 last.
Check yourself Question 1 of 4
Concept Check Time fields recall

How many time fields come before the command in a crontab line?

Recap & Key Takeaways

A crontab line has five time fields for minute, hour, day of month, month, and day of week, followed by the command.
Steps such as */15 and ranges such as 1-5 let you write repeated schedules in one field.
Cron runs commands with a minimal environment and no terminal, so use full paths and redirect output to a log you control.
The lab has no cron daemon, so schedules are practiced as files and checked by their shape.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue