Cron
A system scheduler on Unix-like operating systems that runs commands at specified times, such as every night at 2 a.m. Its schedules are stored in tables called crontabs.
Also known as: крон, crontab
What cron does
Suppose you need to generate a report before your team starts work. Instead of leaving a terminal open or writing a program that waits until morning, you can put the command on a schedule. The cron service runs in the background and starts the command when the system clock matches that schedule. Typical jobs include backups, report generation, and temporary-file cleanup.
Cron is the scheduler. A crontab is a table of jobs, and crontab is also the command used to manage it. Use crontab -e to edit your own table and crontab -l to list it. Jobs in a user's table run with that user's permissions. Linux system tables such as /etc/crontab have an extra field between the schedule and the command to specify which user runs the job.
How to read a cron expression
A standard user crontab entry has five time fields followed by the command:
minute hour day-of-month month day-of-week command
An asterisk matches any allowed value, a comma lists values, a hyphen marks a range, and a slash specifies a step. For example, */15 in the minute field matches minutes 0, 15, 30, and 45 of each hour. It does not mean “wait 15 minutes after the last run finishes.” The standard format has no seconds field; other schedulers that use cron-like syntax may accept a different format.
Example: a weekday report
30 2 * * 1-5 /opt/jobs/build-report.sh >> /var/log/build-report.log 2>&1
Cron starts the script at 2:30 a.m. Monday through Friday. The shell appends standard output to the log file with >> and sends standard error there too with 2>&1. The job's user must be able to execute the script and write to the log. If the shell cannot open the log, it will not start the script.
Common mistakes
- It works in my terminal. A cron job does not necessarily inherit your interactive shell's configuration. Use absolute paths, set required environment variables explicitly, and make the script's working directory explicit if it uses relative paths.
- It started, so it must have worked. Cron starts commands; it does not automatically retry failures or normally make up runs missed while the host was down. For a critical job, monitor successful completion, not just launch. Check which time zone the scheduler uses and how it handles daylight saving time changes.
- The last run will be done by then. If a job takes 20 minutes and is scheduled every 15, two copies can overlap. On a single Linux host, a lock such as
flockcan be used to skip a run while the previous one is still active.
What comes up in interviews
A common question is whether 0 9 1 * 1 means “9 a.m. on the first Monday of the month.” In common Linux cron implementations, when both day fields are restricted, matching either one is enough. This job runs at 9 a.m. on the first of each month and on Mondays. Check the target scheduler's rules before moving an expression between tools.