crontab From Zero: Jobs That Just Run on Time, Five Fields Explained Once
cron is the scheduling daemon that has run on Linux for decades: write a crontab file saying 'run this command at this time', and it handles the rest. This guide runs everything for real on Debian cron 3.0pl1-162 — installing a job, waiting one minute for the trigger, verifying the log, removing the job — plus special strings, five classic mistakes, and a real 3 a.m. log-archiving job.

Some jobs do not need a human watching them. They just need to run on time: back up the database at 3 a.m. every day, clear temporary files every hour, send a weekly report on Monday morning. Who runs them? cron — the scheduling daemon that has been doing this job on Linux for decades. You write a crontab file that says "run this command at this time", and cron takes care of the rest: waking up on schedule, running the command, and collecting the output. No loops to write, no sleep calls, no terminal left open.
Every command in this guide was run for real on Debian cron 3.0pl1-162 (Vixie cron), and the output is pasted verbatim. One honest note about the environment first: this demo sandbox had no cron installed at all — the crontab command simply did not exist. It was installed for the demo: the cron 3.0pl1-162 package was downloaded from deb.debian.org, installed with dpkg -i, and the daemon started directly with /usr/sbin/cron. The install had two small hiccups: a missing pre-dependency (cron-daemon-common) made dpkg refuse at first, and the crontab group had to be created and the spool directory permissions fixed before the daemon would run. On an Ubuntu or Debian server with systemd, none of that is needed — systemctl enable --now cron is enough. Everything below was done after the daemon was running, and behaves exactly like any normally installed cron.
First, the state before anything is installed. A user with no crontab gets this from crontab -l:
$ crontab -l (before: empty) no crontab for root exit=1
"no crontab for root" is a normal state, not an error — it only means this user has no scheduled jobs yet. Note the exit code is 1. Scripts test for the existence of a crontab using that exit code.
Five fields, one command
Each line of a crontab file (apart from comments and blank lines) is one job, in a fixed format: five time fields, followed by the command to run. Left to right they are minute, hour, day of month, month, and day of week. The manual (crontab(5)) gives the ranges: minute 0–59, hour 0–23, day of month 1–31, month 1–12 (names work too, such as jan and mar), day of week 0–7 (0 and 7 are both Sunday; names work too, such as mon, wed, fri).
min hour dom mon dow command * * * * * /tmp/demo281-crontab/tick.sh │ │ │ │ │ │ │ │ │ └── day of week 0-7 (0 and 7 are Sunday) │ │ │ └────── month 1-12 │ │ └────────── day of month 1-31 │ └────────────── hour 0-23 └────────────────── minute 0-59
An asterisk * means "every": * in the minute field is every minute. Fields also accept ranges (8-11 means 8, 9, 10, 11), lists (1,15 means the 1st and the 15th), and steps (*/5 means every 5 units, 0-23/2 means every other hour). They combine: 0-4,8-12 means hours 0 to 4 plus 8 to 12. A useful way to remember it: asterisk is "all", hyphen is "a run", comma is "pick a few", slash is "skip some".
There is also a set of shorthands called special strings, prefixed with @, which replace the five fields: @reboot runs once after reboot, @yearly and @annually run once a year (0 0 1 1 *), @monthly once a month (0 0 1 * *), @weekly once a week (0 0 * * 0), @daily once a day (0 0 * * *), @hourly once an hour (0 * * * *). Verified by installing them:
$ crontab dailytab @daily /tmp/demo281-cron/tick.sh */5 * * * * /tmp/demo281-cron/tick.sh
Both @daily and */5 * * * * install and list back normally. @daily fits "run at midnight" jobs; */5 fits "every five minutes" jobs. Neither is better — only more convenient for the case at hand.
The first scheduled job: install to trigger
Start with a script to schedule. It does one thing: append a timestamped tick line to a log file:
$ cat mycrontab # minute hour dom month dow command * * * * * /tmp/demo281-crontab/tick.sh
This job means: every minute, run tick.sh. Install it:
$ crontab mycrontab exit=0 $ crontab -l # minute hour dom month dow command * * * * * /tmp/demo281-crontab/tick.sh exit=0
crontab mycrontab installs the file as the current user's crontab; crontab -l lists what is installed. Note that the comment line survives in the listing — comments in a crontab file are not discarded.
Checking the syntax before installing is a good habit. This build of cron uses -n (dry run: check the syntax, install nothing):
$ crontab -n mycrontab The syntax of the crontab file was successfully checked. exit=0
One version difference to note: the crontab(5) manual page documents -T for testing syntax, but the Debian cron 3.0pl1-162 installed here does not accept -T — it answers "invalid option". It uses -n instead, with the same effect: a good file prints "The syntax of the crontab file was successfully checked." with exit code 0; a bad file errors with exit code 1 and installs nothing. Different cron implementations (Vixie cron, cronie, systemd-cron) disagree on this option, so check your own machine's manual before typing it.
What happens with a bad file? Deliberately write one with only four fields:
$ crontab badtab (4 fields: rejected, nothing installed) "badtab":1: bad day-of-week errors in crontab file, can't install. exit=1
A four-field job will not install, and the error reads "bad day-of-week" — cron parses five fields, and the missing one fails validation as the day of week. The important part is the second line: "errors in crontab file, can't install." The whole file is rejected and the old crontab stays untouched. That is a deliberate safety design: installing a crontab is atomic — all or nothing, never half installed. Listing afterwards confirms the old job is still there:
$ crontab -l # minute hour dom month dow command * * * * * /tmp/demo281-crontab/tick.sh exit=0
Then wait. cron examines the table every minute. At 01 seconds into the next minute, tick.sh ran and the log gained a line:
$ cat tick.log 2026-12-01 13:08:01 tick $ grep -c tick tick.log 1
The timestamp is 13:08:01 — the job fired at 13:08:00 and the script wrote its line one second later. The exact time differs on every run, which is expected; the stable part is the format: date, time, tick. Finally, clean up:
$ crontab -r exit=0 $ crontab -l (after removal) no crontab for root exit=1
crontab -r deletes the entire crontab, and crontab -l afterwards is back to "no crontab for root". -r asks for no confirmation, so think before pressing enter — some builds of crontab support -i, which asks first, and it is worth adding if your fingers are slippery.
Five things that go wrong
First, comments cannot share a line with a command. The manual states it plainly: only a line whose first non-space character is # is a comment, and a comment on the same line as a command is treated as part of the command. So * * * * * backup.sh # daily backup makes cron execute backup.sh # daily backup, passing the comment as arguments. Comments get their own line, above the job.
Second, day of month and day of week are OR, not AND. This is the most famous crontab trap. The manual says: if both the day-of-month and day-of-week fields are restricted (neither contains *), the command runs when either field matches. For example, 30 4 1,15 * 5 runs at 4:30 a.m. on the 1st and 15th of each month, plus every Friday. If you want "the 1st of the month, but only when it is a Friday", the crontab syntax cannot express it — test the date inside the command instead.
Third, % is special. A % in the command is converted to a newline, and everything after the first % is sent to the command as standard input. The manual's example: 0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?% — the text after % becomes the mail body. If your command genuinely needs a %, such as the format string in date +%Y-%m-%d, escape it: date +\%Y-\%m-\%d. Otherwise cron truncates the command at the % and feeds the rest as input.
Fourth, the environment is not your login shell. cron runs jobs with SHELL set to /bin/sh (not your bash or zsh), HOME and LOGNAME taken from /etc/passwd, and a very small PATH — usually just /usr/bin:/bin. The most common failure of all is "it works when I run it by hand and fails in cron", and nine times out of ten the cause is a command missing from PATH or a relative path that resolves nowhere. Fix it two ways: set PATH=/usr/local/bin:/usr/bin:/bin at the top of the crontab file, or use absolute paths in the command. The safest pattern is to put the logic in a script file with #!/usr/bin/env bash on its first line, and only reference the script's absolute path in the crontab. tick.sh in this guide does exactly that.
Fifth, where does the output go. cron mails a job's output (stdout and stderr) to the crontab's owner, controlled by MAILTO: set it to an address and mail goes there, set it empty and no mail is sent, leave it unset and mail goes to the owner. Most servers have no mail service configured, so the output vanishes. The right approach is to redirect it yourself: * * * * * /path/job.sh >> /var/log/job.log 2>&1, appending both streams to a log file. Remember to rotate that log file, or in a few years it will fill the disk — which is the story the df/du guide already told.
The */35 trap: steps stay inside their field
Steps only operate within the field they are attached to. The manual warns about this explicitly: */35 in the minute field does not run every 35 minutes — it runs twice an hour, at minute 0 and minute 35. The step walks 0 to 59 in jumps of 35: 0, then 35, then 70 is out of range, so it stops. For a true "every 35 minutes", the manual's workaround is to test inside the command: * * * * * if [ $(expr \( $(date +%s) / 60 \) % 35) = 0 ]; then ...; fi — fire every minute, but only really execute when the minute count divides by 35. Similarly, */23 in the hours field runs twice a day (midnight and 23:00), not every 23 hours. The trap has caught many people. Remember one sentence: a step jumps inside its field, it does not count time intervals.
A real job: archiving logs every night at 3 a.m.
Syntax alone is not enough, so here is a complete real job: every night at 3 a.m., compress the logs in /var/log/myapp into an archive, and delete archives older than 30 days. Write the script first, put it in the crontab, and verify with a one-minute job — do not write "3 a.m. daily" and wait until tomorrow to discover a typo.
#!/usr/bin/env bash # /usr/local/bin/archive-logs.sh set -euo pipefail DAY=$(date +%F) mkdir -p /var/backups/myapp tar -czf "/var/backups/myapp/$DAY.tar.gz" -C /var/log myapp find /var/backups/myapp -name '*.tar.gz' -mtime +30 -delete echo "$(date '+%F %T') archived $DAY" >> /var/log/archive.log
Three points about this script. First, set -euo pipefail makes it stop immediately on error rather than blundering on — at 3 a.m. nobody is watching, and stopping beats failing silently. Second, every path is absolute, and tar uses -C /var/log to change directory before archiving the relative path myapp, so the archive contains clean paths. Third, find ... -mtime +30 -delete removes old archives in the same script — archiving without cleanup eventually fills the disk. The script was verified in a scratch directory: it ran with exit code 0, produced a correct tarball, deleted a 40-day-old archive, and wrote its log line.
Verify the script itself with a one-minute job first:
* * * * * /usr/local/bin/archive-logs.sh
Install it, wait a minute, check that /var/log/archive.log gained a line and reported no errors. Once that is confirmed, change the line to the real schedule:
0 3 * * * /usr/local/bin/archive-logs.sh >> /var/log/archive-cron.log 2>&1
0 3 * * * is 3:00 a.m. every day. Output goes to archive-cron.log, both stdout and stderr. This "verify every minute, then switch to the real time" flow is the standard way to use cron: verification costs one minute, debugging costs a whole day. Do not skip the step.
System crontabs: /etc/crontab and /etc/cron.d
Everything so far has been a user's crontab — the kind crontab -e edits. The system has two more places for scheduled jobs: /etc/crontab and the /etc/cron.d/ directory. They are system jobs, and their format has one extra field — the username that runs the command, before the command itself:
# /etc/cron.d/myapp (a system job — note the extra username field) MAILTO=root * * * * * root touch /tmp/file
That is the manual's own example. A user crontab is "five fields plus command"; a system crontab is "five fields plus username plus command". System jobs are usually written by root or by packages — Debian's logrotate and man-db both drop their jobs into /etc/cron.d. Ordinary users should leave the system files alone: they lack the permission, and their own jobs belong in their own crontab. There are also /etc/cron.daily and /etc/cron.hourly, directories of executable scripts the system runs once a day and once an hour. They are a separate mechanism from crontab — do not confuse them.
Where the file lives, and how to back it up
A user's crontab lives in /var/spool/cron/crontabs/, one file per user, named after the user. The directory is locked down; ordinary users cannot read each other's files. Backing up your own crontab is one command:
$ crontab -l > ~/my-crontab-backup.txt $ cat ~/my-crontab-backup.txt
Restoring is just as simple: crontab ~/my-crontab-backup.txt. Put that backup line in your migration checklist before switching machines or reinstalling. Scheduled jobs fail silently when lost — nothing errors, the 3 a.m. backup simply does not happen, and you find out the next day. By then it is too late.
One more time-related trap: daylight saving time. The manual states that jobs scheduled during the "missing hour" of the spring transition never run, and jobs scheduled during the repeated hour of the autumn transition run twice. That does not affect readers in China, which has no daylight saving time — but if your server is abroad or serves users abroad, avoid scheduling jobs between 2 a.m. and 3 a.m. on transition days. Time zones can also be set per crontab: CRON_TZ=Japan makes the jobs run on Japan time — but the daemon writes its log timestamps in its own local time zone, so do not confuse the two.
Troubleshooting checklist: the job did not run
The most infuriating thing about scheduled jobs is that they fail silently. No popup, no error — you only discover the next day that the backup did not happen and the report was never sent. Work through this list in order; it finds the cause nine times out of ten.
First, is the daemon running. cron is a long-lived process that must be started after every reboot. On a systemd machine, systemctl status cron should show Active: active (running); if it shows inactive (dead), run systemctl enable --now cron. This demo sandbox does not run systemd as process 1, so systemctl status cron reports inactive (dead) here — but cron was started directly with /usr/sbin/cron and ps aux | grep cron shows the process running. Two ways of starting, two ways of checking; do not be frightened by systemctl's "dead" in a container.
Second, is the crontab installed. Run crontab -l and confirm the line you want is really there. Many people edit the file and forget to install it: they changed mycrontab but never ran crontab mycrontab, so nothing runs. Read it back with crontab -l after every change.
Third, are the time fields right. Read the line field by field: minute 0–59, hour 0–23, day of week 0–7. The classic mistake is writing 0 0 1 * * (the 1st of every month) while meaning "every Monday" — that is 0 0 * * 1. And remember the */35 trap from above.
Fourth, does the script run by hand. Execute the command itself in a terminal: /usr/local/bin/archive-logs.sh, and watch for errors. If it works by hand but not in cron, the cause is almost certainly the environment: too small a PATH, a relative path that resolves nowhere, a wrong HOME. If it fails by hand too, the problem is not cron — fix the script first.
Fifth, what is in the logs. Where did the job's output go? If you redirected it to a log file as recommended, read that file; if you did not, the output went to mail, which on a machine without a mail service means it vanished. System-level records on a systemd machine come from journalctl -u cron, which shows every trigger — the site's journalctl guide covers that command. In this sandbox, journald collected no cron entries, which is a container environment quirk, not a cron problem; on a normal server journalctl -u cron shows the trigger records.
When not to use cron
cron fits jobs that run on a schedule and then exit: backups, cleanup, reports, sync. It does not fit four situations. First, sub-minute precision: the smallest granularity is one minute; every 30 seconds or every 10 seconds is impossible. Second, retries and dependencies: if the downstream job cannot start until the upstream one finishes, cron cannot orchestrate that — use a workflow tool or systemd service dependencies. Third, overlapping runs: a job that takes 90 seconds and fires every minute will start a second instance while the first is still running, and the two will step on each other — add locking yourself (flock is the standard tool). Fourth, very long jobs: cron does not care how long a job takes, and if the machine reboots or the job is killed, it will not run it again to catch up.
Compared with systemd timers, cron's strengths are simplicity, ubiquity, and one file for everything; its weaknesses are fewer features: no "run now to catch up on missed runs" (persistent), no on-demand triggers, and logs you manage yourself. The site's systemd user-services guide already covers the systemd side, and timers are a topic for another guide. Which to choose depends on the machine: cron is standard on old-school servers and containers, systemd timers feel more natural on modern desktops and cloud hosts. Knowing both is the complete answer.
Scope and summary
How this guide relates to the site's earlier ones: the journalctl guide covers "reading system logs", the systemd user-services guide covers "running services with your user session", the env guide covers "how environment variables take effect". crontab does none of those — it does one thing: run a command on time. Need to "run something on a schedule", read this guide; need to "see what the run left behind in the logs", read journalctl; need to "understand why the environment inside the command is wrong", read env.
Five things to remember: the five fields are minute, hour, day of month, month, day of week — asterisk is all, hyphen is a run, comma picks a few, slash skips some; when day of month and day of week are both restricted, they are OR, not AND; check the syntax with crontab -n before installing, and a bad file installs nothing at all; use absolute paths in commands and redirect output to a log file; and crontab -r deletes the whole crontab, so think before pressing enter. The next guide in this series will cover watch — the command that refreshes the screen every few seconds — which complements cron's "every minute" perfectly: one runs things on a schedule, the other keeps watching.
Comments (0)
Sources
Related articles
ping does exactly one thing: send an ICMP echo request, wait for the echo reply, and report the round-trip time and packet loss. This guide runs every command for real on iputils ping 20240117 — packet count, interval, quiet mode, packet size, timestamps, IPv4 vs IPv6, the three failure modes, exit codes 0/1/2 — with output pasted verbatim.
cut does one thing: split each line into fields on a delimiter and output the fields you ask for. This guide runs every command for real on GNU coreutils 9.4 — field lists and ranges, CSV and output delimiters, character slicing, --complement, the -s rule for lines without a delimiter, log-column statistics, and the quoted-comma limit — with the output pasted verbatim.
grep does one thing: find lines in text that match a pattern. This guide runs every command for real on GNU grep 3.11 — line numbers, case-insensitive search, recursion, context lines, whole-word matching, the three pattern modes (BRE, -E, -F), anchors and alternation, quiet mode for scripts, and binary files — with the output pasted verbatim.