Lesson 6 of 7 · 11 min · Text and scheduling
Scheduling with cron and systemd timers
What you will learn
- The five time fields of a crontab line and the special shortcuts
- User crontabs versus
/etc/cron.dand/etc/cron.daily - Why cron jobs behave differently from your shell, and how to fix it
- Logging, mail, overlapping runs and time zones
- When systemd timers are the better choice
In simple words
cron is an alarm clock for commands. You tell it "run this backup at 2 a.m. every day", and it does, with no one logged in. This lesson shows how to write that timetable, and why a job that works when you run it by hand can fail quietly when cron runs it.
Words to know
- cron: the background service that runs commands on a timetable.
- crontab: the file where you list your scheduled jobs, one per line.
- Schedule fields: five values on each line: minute, hour, day of month, month and day of week.
- systemd timer: a newer way to schedule jobs, with better logging and control.
What cron is
cron is a daemon that runs commands on a schedule. Each user can have a crontab (a table of jobs): edit it with crontab -e, list it with crontab -l. A line has five time fields and then the command:
# ┌ minute (0-59)
# │ ┌ hour (0-23)
# │ │ ┌ day of month (1-31)
# │ │ │ ┌ month (1-12)
# │ │ │ │ ┌ day of week (0-7, 0 and 7 are Sunday)
# │ │ │ │ │
*/5 * * * * /usr/local/bin/job.sh >> /var/log/job.log 2>&1
That line runs every five minutes. Each field accepts:
| Syntax | Meaning | Example |
|---|---|---|
* |
every value | * * * * * every minute |
5 |
one value | 5 * * * * at minute 5 of every hour |
1,15 |
a list | 0 8 1,15 * * on the 1st and 15th at 08:00 |
1-5 |
a range | 0 9 * * 1-5 weekdays at 09:00 |
*/10 |
every Nth | */10 * * * * every 10 minutes |
Some cron versions also understand @reboot, @hourly, @daily, @weekly, @monthly. Examples:
30 2 * * * /usr/local/bin/backup.sh # every day at 02:30
0 */6 * * * /usr/local/bin/sync.sh # every 6 hours
0 9 * * 1-5 /usr/local/bin/report.sh # 09:00 on weekdays
@reboot /usr/local/bin/after-boot.sh
Be careful with day of month and day of week: when both are restricted, the job runs when either matches, not both. Check a schedule you are unsure about with a calculator such as crontab.guru before you rely on it.
Where jobs live
| Place | Format | Use |
|---|---|---|
crontab -e (per user) |
5 time fields + command | a person's own jobs |
/etc/crontab and /etc/cron.d/* |
5 time fields + user + command | system and packaged jobs, easy to deploy as a file |
/etc/cron.hourly, cron.daily, cron.weekly, cron.monthly |
executable scripts (no schedule inside) | simple periodic tasks; the exact time is chosen by the system |
Files in /etc/cron.d must be owned by root, not writable by others, and their names must not contain dots on Debian-family systems, or they are silently ignored.
Why "it works in my terminal but not in cron"
This is the most common cron problem. A cron job does not run in your login shell. Real output of a job that printed its environment:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
HOME=/root
SHELL=/bin/sh
(The long PATH here was inherited from the container that started cron; on a normal installation cron's default PATH is very short, often just /usr/bin:/bin.) The point is:
- The shell is
/bin/shby default, not Bash. Bash-only syntax ([[ ]], arrays) fails unless the script has a#!/usr/bin/env bashline and is run as a program, or the crontab setsSHELL=/bin/bash. - The
PATHis minimal. Use full paths for commands and programs (/usr/bin/docker), or setPATH=at the top of the crontab. - No terminal, no profile files (
~/.bashrcis not read), so variables likeJAVA_HOMEmust be set in the script. - The working directory is the user's home, not where the script lives.
cdexplicitly or use absolute paths. - A
%character is special in a crontab command: it is turned into a newline, unless escaped as\%. This breaksdate +%Fin a crontab line. Writedate +\%F, or put the logic inside a script file.
To reproduce the problem, run the command the way cron does: env -i /bin/sh -c '/usr/local/bin/job.sh'.
Output, mail and logs
Anything a job prints is mailed to the user (if a mail system exists; set MAILTO=you@example.com, or MAILTO="" to disable). On most servers nobody reads that mail, so redirect output yourself:
*/5 * * * * /usr/local/bin/job.sh >> /var/log/job.log 2>&1
>> appends; 2>&1 includes errors. Whether cron started the job at all is recorded in the system log: grep CRON /var/log/syslog on Debian/Ubuntu (or journalctl -u cron), /var/log/cron on RHEL family.
Overlapping runs
If a job takes longer than its interval, the next run starts while the first is still going, which can corrupt data or exhaust the machine. Prevent it with flock. We tested: while one copy holds the lock, the second is skipped.
*/5 * * * * flock -n /var/lock/job.lock /usr/local/bin/job.sh >> /var/log/job.log 2>&1
got lock
second run skipped (locked)
(The first line is from the running job; the second shows flock -n giving up immediately when the lock is taken.) Also write jobs so a failed run is safe to repeat.
Time zones and missed runs
Cron uses the server's time zone (timedatectl), which on cloud machines is often UTC. A job "at 09:00" on a UTC server runs at 14:30 Indian time. Some cron versions support CRON_TZ=Asia/Kolkata. Daylight saving does not apply in India but does elsewhere, which can skip or double a job in the night hours.
Cron does not catch up on missed runs: if the machine was off at 02:30, that day's backup simply does not happen. anacron (used for cron.daily on laptops) exists to catch up; systemd timers can do it too.
systemd timers
A timer is a unit that starts a service on a schedule. It needs two small files (see the systemd lesson in the Linux course):
# /etc/systemd/system/backup.service
[Unit]
Description=Nightly backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh -k 7 /srv/app /var/backups/app
# /etc/systemd/system/backup.timer
[Unit]
Description=Nightly backup timer
[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
RandomizedDelaySec=300
[Install]
WantedBy=timers.target
Enable with sudo systemctl enable --now backup.timer and check with systemctl list-timers.
| cron | systemd timer | |
|---|---|---|
| Logs | you redirect them | automatically in the journal |
| Missed runs | skipped | Persistent=true runs them on next boot |
| Overlap | you add flock |
a service is not started twice while active |
| Environment | minimal, easy to get wrong | defined in the unit, can sandbox and limit resources |
| Setup | one line | two files |
| Available | everywhere, including containers and old systems | systemd systems |
For a single quick job, cron is fine. For anything important on a systemd machine, prefer a timer. In Kubernetes neither exists: you use a CronJob resource, which the Kubernetes course covers.
Common mistakes
- Relative paths and the minimal
PATH. - Unescaped
%in the command. - No output redirection, so failures vanish.
- No lock on a job that can run long.
- Server in UTC while you think in local time.
- Editing
/etc/crontabformat wrongly: per-user crontabs have no user field, system files do. - Never checking that the job actually ran (look at the log and at its output files).
Try it yourself
- Write a crontab line that runs a script every weekday at 18:30 and appends its output to a log. Install it with
crontab -eand confirm withcrontab -l. - Create a job that writes
envto a file. Compare with your interactiveenvand list three differences. - Put
date +%Finto a crontab line unescaped and see what happens; then fix it. - Convert the job into a systemd service and timer and inspect it with
systemctl list-timersandjournalctl -u.
Check yourself
What does */15 9-17 * * 1-5 mean?
Every 15 minutes between 09:00 and 17:59, Monday to Friday.
A script runs fine by hand but fails under cron. What are the usual causes?
A different, minimal environment: PATH, shell (sh not Bash), working directory and missing variables; also permissions of the cron user and unescaped %. Reproduce with env -i /bin/sh -c '...' and log the output.
How do you keep a long-running job from overlapping with its next run?
Wrap it in flock -n lockfile command, so the second instance exits immediately while the first holds the lock.
Name two advantages of a systemd timer over cron.
Logs go to the journal automatically, and Persistent=true catches up on missed runs. Also dependency and resource control, and no double-start of an active service.
Interview questions
What happens to a cron job scheduled while the server is off?
It is skipped; cron does not catch up. Use anacron or a systemd timer with Persistent=true if that matters.
Where do you look to see whether cron ran your job?
The cron log lines (grep CRON /var/log/syslog or journalctl -u cron), then the job's own redirected log.
How do you schedule something every 90 minutes in cron?
A single line cannot express it (fields are per hour). Run a job every 30 minutes and have it check elapsed time, or use a systemd timer with OnUnitActiveSec=90min.
Summary
- A crontab line is
minute hour day month weekday command; usecrontab -e,/etc/cron.d(with a user field) or thecron.dailydirectories. - Cron runs with
/bin/sh, a minimalPATHand no profile: use full paths, set variables, escape%, redirect output. - Prevent overlap with
flock; remember the server time zone; cron does not catch up on missed runs. - systemd timers add logging, catch-up and dependencies; Kubernetes uses
CronJob.
Going deeper
- crontab(5): the exact file format, environment and the
%rule. - systemd.timer(5): timer options including
OnCalendarandPersistent. - cron(8): the daemon, its logging and how it reads crontab files.
See how much of this you can explain
A free mock interview asks you new questions on Shell scripting with Bash for DevOps Engineer and scores every answer, with what to fix.