How does cron work in Linux for scheduling jobs?
Learn how cron schedules jobs in Linux: the five crontab time fields, the cron daemon, user vs system crontabs, and how job output is handled — with examples.
Expected Interview Answer
Cron is a time-based job scheduler in Linux run by the cron daemon (crond), which wakes every minute and runs any command whose schedule matches the current time. Schedules are defined in crontab files using five time fields plus the command.
Each crontab line has five fields — minute, hour, day-of-month, month, and day-of-week — followed by the command to execute. Users edit their own jobs with crontab -e, while system-wide schedules live in /etc/crontab and /etc/cron.d, plus the /etc/cron.daily, .hourly, .weekly and .monthly directories run by run-parts. Cron reads these files, compares each schedule to the clock once per minute, and executes matches in a minimal environment, mailing any output to the user unless redirected.
- Automates repetitive tasks without manual intervention
- Fine-grained minute-level scheduling control
- Per-user and system-wide job separation
- Runs in the background with low overhead
- Standard, dependable, available on nearly every Linux system
AI Mentor Explanation
Cron is like the umpire's clock at a timed cricket match: it checks the time continuously and, the moment a scheduled event is due, triggers it — drinks break, innings change, close of play. Each event has a fixed slot on the schedule, and the umpire fires it exactly when the clock matches, without anyone reminding him ball by ball.
Step-by-Step Explanation
Step 1
The cron daemon starts
crond launches at boot and stays running, waking up roughly once every minute to evaluate schedules.
Step 2
Cron reads crontab files
It parses per-user crontabs (crontab -e), plus /etc/crontab and files in /etc/cron.d and the cron.* directories.
Step 3
Match the schedule to the clock
For each job it compares the five time fields — minute, hour, day-of-month, month, day-of-week — against the current time.
Step 4
Execute matching jobs
Any job whose fields all match is run in a minimal shell environment as the owning user.
Step 5
Handle output
Standard output and error are emailed to the user unless redirected, e.g. appended to a log or sent to /dev/null.
What Interviewer Expects
- Correct meaning of the five crontab time fields
- Difference between user crontabs and system cron directories
- Knowledge that crond checks schedules once per minute
- Awareness that cron runs in a minimal environment
- How job output is delivered and how to redirect it
Common Mistakes
- Mixing up the day-of-month and day-of-week fields
- Assuming the login shell's PATH and environment are available
- Forgetting cron cannot schedule below one-minute granularity
- Not redirecting output, then wondering where errors went
- Editing crontab files directly instead of using crontab -e
Best Answer (HR Friendly)
“Cron is Linux's built-in scheduler that runs commands automatically at set times. You write a small schedule saying when a job should run — say every night at 2am — and a background service checks the clock every minute and runs the job whenever the time matches.”
Code Example
# minute hour day-of-month month day-of-week command
# 0-59 0-23 1-31 1-12 0-7 (0/7=Sun)
# Run a backup every day at 02:30, log output
30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
# Run every 15 minutes
*/15 * * * * /usr/local/bin/health-check.sh
# Run at 09:00 every Monday
0 9 * * 1 /usr/local/bin/weekly-report.sh
# Edit the current user's crontab
# crontab -e
# List it
# crontab -lFollow-up Questions
- How do you run a job every 15 minutes using a crontab entry?
- Why might a script work in your shell but fail under cron?
- What is the difference between cron and anacron?
- How would you schedule sub-minute tasks that cron cannot handle?
- Where does cron send a job's output by default?
MCQ Practice
1. In a standard crontab line, what does the third field represent?
The five fields in order are minute, hour, day-of-month, month, and day-of-week, so the third field is the day of month.
2. How often does the cron daemon check whether jobs are due?
crond wakes approximately once per minute and runs any job whose schedule matches the current time, giving cron minute-level granularity.
3. What does the schedule */15 * * * * mean?
The */15 in the minute field means 'every 15 minutes', with all other fields as wildcards, so the job runs four times an hour.
Flash Cards
What are the five crontab time fields in order? — Minute, hour, day-of-month, month, day-of-week — followed by the command to run.
Which command edits the current user's crontab? — crontab -e opens the user's crontab for editing; crontab -l lists it.
How often does crond evaluate schedules? — About once every minute; cron cannot schedule tasks more finely than one-minute granularity.
Where does cron send job output by default? — To the owning user's mail; redirect with >> logfile 2>&1 to capture it instead.