What Is a Cron Job? How Cron Works, Syntax and Examples
What is a cron job? A command that Linux runs automatically on a schedule. Learn how cron works, how to edit your crontab, common schedules and the gotchas.
Updated
A cron job is a command or script that a Unix-like system runs automatically on a repeating schedule, such as every five minutes or at 03:00 each night. The schedule and the command sit together on one line of a file called a crontab, and a background service named cron reads those files and starts each job when its time comes. It is the standard way to automate backups, cleanups, reports and other routine work on Linux and macOS.
How cron runs
Cron is a daemon, a process that starts at boot and stays running. Once a minute it wakes up, compares the current time with every entry in every crontab, and launches the commands whose schedule matches. It then goes back to sleep until the next minute. That is why one minute is the smallest interval cron can express.
Each job runs as the user who owns the crontab, in a new non-interactive shell, with no terminal attached. Cron does not wait for a job to finish and keeps no record of whether an earlier run succeeded, overlapped or was missed.
Anatomy of a crontab line
A crontab entry is five schedule fields followed by the command:
30 2 * * 1 /home/ada/bin/backup.sh
| Field | Allowed values | In the example |
|---|---|---|
| Minute | 0-59 | 30 |
| Hour | 0-23 | 2 |
| Day of month | 1-31 | * (any) |
| Month | 1-12 | * (any) |
| Day of week | 0-6, Sunday is 0 | 1 (Monday) |
So the line means "run backup.sh at 02:30 every Monday". Four operators cover nearly every schedule: * for every value, , for a list, - for a range and / for a step, as in */5. The cron expression cheat sheet goes through the syntax in depth, including the rule that day of month and day of week combine with OR.
Editing your crontab
You do not edit crontab files directly. The crontab command manages them:
crontab -eopens your crontab in an editor. When you save and quit, cron installs the new version, with no restart needed.crontab -lprints your current crontab.crontab -rdeletes your entire crontab without asking. Theeandrkeys are neighbors, so keep a copy:crontab -l > crontab.bak.
Lines starting with # are comments.
Besides per-user crontabs there are system ones. /etc/crontab and files in /etc/cron.d use the same format with one extra field, the user to run as, between the schedule and the command. Many distributions also provide /etc/cron.hourly, cron.daily, cron.weekly and cron.monthly directories: drop an executable script in and it runs at that frequency.
Common schedules
| Schedule | Expression |
|---|---|
| Every minute | * * * * * |
| Every 5 minutes | */5 * * * * |
| Every 15 minutes | */15 * * * * |
| Every hour, on the hour | 0 * * * * |
| Every day at midnight | 0 0 * * * |
| Every day at 03:30 | 30 3 * * * |
| Weekdays at 09:00 | 0 9 * * 1-5 |
| Every Sunday at 04:00 | 0 4 * * 0 |
| First day of the month | 0 0 1 * * |
| At startup | @reboot |
A complete entry for "every 5 minutes" looks like */5 * * * * /usr/bin/python3 /opt/app/sync.py >> /var/log/sync.log 2>&1. Most Linux crons also accept the shortcuts @hourly, @daily, @weekly, @monthly and @yearly in place of the five fields.
Before saving a schedule, check it. The Cron Expression Builder lets you set each of the five fields or start from a preset, describes the result in plain English, and lists the next five run times. Those times are calculated in your own computer's time zone, so adjust if the server uses a different one.
Gotchas
Most "my cron job does nothing" problems come from the gap between your terminal and cron's bare environment.
A minimal environment
Cron does not load your .bashrc or .profile. The PATH is typically just /usr/bin:/bin, and variables you rely on, such as API keys or NODE_ENV, are absent. A script that works when you type it can fail under cron with "command not found".
Fixes: use absolute paths for every program and file, set variables at the top of the crontab with lines like PATH=/usr/local/bin:/usr/bin:/bin, or have the job call a wrapper script that sets up what it needs. The default shell is /bin/sh, not bash; add SHELL=/bin/bash if the command uses bash features.
Time zone
Cron uses the system's local time zone. Many servers and containers are set to UTC, so 0 9 * * * fires at 09:00 UTC, not 09:00 where you are. Run date on the machine to see what it thinks the time is. Some cron implementations, including cronie on Red Hat based systems, accept a CRON_TZ=Europe/Berlin line to set the zone for the entries that follow.
In zones with daylight saving time, a job scheduled inside the hour that is skipped or repeated can be missed or run twice, depending on the implementation. Scheduling in UTC, or outside the 01:00 to 03:00 window, avoids it.
Output goes nowhere obvious
Cron tries to email whatever a job prints to the crontab's owner. On machines without a mail setup, that output is lost. Redirect it yourself: >> /var/log/myjob.log 2>&1 appends both normal output and errors to a file. MAILTO="" at the top of the crontab turns the email attempt off.
The percent sign
In a crontab command, an unescaped % is treated as a line break, and everything after it is sent to the command as input. date +%F therefore breaks. Write date +\%F, or move the command into a script.
Overlapping and missed runs
If a job takes six minutes and runs every five, cron starts a second copy while the first is still going. Wrap it with flock -n /tmp/myjob.lock so a new run exits when the lock is held. If the machine is off at the scheduled time, the run is skipped; anacron or a systemd timer with Persistent=true can catch up after boot.
To debug, make the job log everything, then temporarily schedule it for every minute. On Debian and Ubuntu,grep CRON /var/log/syslogshows whether cron started it; on systemd systems, tryjournalctl -u cronorjournalctl -u crond.
Cron outside Linux servers
The five-field expression has spread well beyond the original daemon. GitHub Actions schedule triggers, Kubernetes CronJobs and most hosted schedulers use it, usually in UTC.
When a job records when it ran, it often stores a Unix timestamp. The Unix timestamp converter turns those values into readable dates so you can check that runs happened when expected.
Try it
Build your schedule in the Cron Expression Builder, confirm the English summary and the next five run times, then paste the expression into crontab -e in front of your command with absolute paths and a log redirect.