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.
30 2 * * * /usr/local/bin/backup.shRead 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.
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1The 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.
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.
echo "*/5 * * * * /workspace/backup.sh" > mycron
cat mycronThe 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.