Crontab: syntax, examples, and why a cron job doesn't run
Crontab's five fields, @reboot and @daily, crontab -e/-l/-r, where output goes, PATH and % pitfalls, CRON_TZ, and an AI agent on cron with flock.
A crontab line is five time fields and a command: minute (0–59), hour (0–23), day of month (1–31), month (1–12) and day of week (0–7, where 0 and 7 are Sunday), so 30 2 * * * /home/you/bin/job.sh runs at 02:30 every day. Edit yours with crontab -e, list it with crontab -l; crontab -r deletes the whole crontab without asking. Output is mailed to you, which on a server with no mail program means it is thrown away, so add >> /home/you/job.log 2>&1. A job that works in your terminal and fails in cron usually trips on cron’s environment: sh rather than your shell, a PATH of /usr/bin:/bin on Debian and Ubuntu, none of your .bashrc, a % that cron turns into a newline, and the system’s time zone.
The five fields
# ┌──────── minute (0-59)
# │ ┌────── hour (0-23)
# │ │ ┌──── day of month (1-31)
# │ │ │ ┌── month (1-12, or jan-dec)
# │ │ │ │ ┌ day of week (0-7, 0 and 7 are Sunday, or sun-sat)
# │ │ │ │ │
# * * * * * command to runCopiedEach field takes a number, a * for every value, a range such as 1-5, a list such as 6,18, or a step: */15 in the minute field is every fifteen minutes, and 1-9/2 is 1, 3, 5, 7 and 9. Months and days also take their first three letters, but Debian and Ubuntu’s cron does not allow names in a range or a list, where cronie does; numbers work in both. As of December 2026 both common implementations read the lines below the same way:
| Line | Runs |
|---|---|
*/15 * * * * | Every 15 minutes |
0 * * * * | Every hour, on the hour |
30 2 * * * | 02:30 every day |
0 9 * * 1-5 | 09:00, Monday to Friday |
0 6,18 * * * | 06:00 and 18:00 |
0 0 1 * * | Midnight on the first of each month |
0 9 1 * mon | 09:00 on the first of the month and every Monday |
The last line catches people out. When both day fields are set, the job runs when either one matches, not both, as the crontab(5) manual says: “the command will be run when either field matches the current time.” To run only on the first Monday of the month, leave the day of month as * and have the command check the date itself.
@reboot, @daily and the other shortcuts
| String | Same as |
|---|---|
@reboot | Once, when cron starts at boot |
@hourly | 0 * * * * |
@daily, @midnight | 0 0 * * * |
@weekly | 0 0 * * 0 (midnight on Sunday) |
@monthly | 0 0 1 * * |
@yearly, @annually | 0 0 1 1 * |
@reboot runs when the cron daemon starts, which the manual warns may be before other services are up, the network included. Debian and Ubuntu’s cron runs those jobs once per boot and skips them if you only restart cron. For an agent or a server that should start with the machine and stay up, a systemd service is the better tool: a user service with lingering on starts at boot and restarts when it crashes, which @reboot never does.
crontab -e, -l and -r
crontab -e
crontab -l
crontab -l > ~/crontab.backupCopiedcrontab -e opens your crontab in an editor and installs it when you save and quit; on Debian and Ubuntu the editor comes from sensible-editor, so EDITOR=nano crontab -e picks one for that time. crontab -l prints it, and the third line is the backup to take before any change you might regret. Cron checks every minute, so there is nothing to restart after an edit.
Two commands overwrite or delete without asking. crontab -r removes your entire crontab at once, in Debian and Ubuntu’s cron and in cronie alike, and the r sits next to the e of crontab -e. Add -i (crontab -i -r) to be asked first. And crontab somefile replaces the whole crontab with that file, so a file holding one new line wipes every other job. With the backup above, either is undone by:
crontab ~/crontab.backupCopiedTo check a file before installing it, Debian 13’s crontab has -n (a dry run) and cronie’s has -T. Root edits another user’s crontab with crontab -u name -e. Never edit the stored files under /var/spool/cron/ directly; cron reads them, crontab writes them.
System-wide jobs live in /etc/crontab and in files under /etc/cron.d/, which take a sixth field, the user to run as, between the time and the command. On Debian and Ubuntu a file in /etc/cron.d/ is ignored if its name contains a dot, so agent.cron never runs while agent-nightly does. Every crontab, yours included, needs a newline after its last line, or cron treats it as broken.
Where the output goes
Whatever a job prints, on standard output or standard error, cron mails to the crontab’s owner, or to the address in a MAILTO= line at the top of the crontab. With MAILTO="" nothing is mailed. A cloud server often has no mail program to hand the output to, and then Debian and Ubuntu’s cron logs No MTA installed, discarding output and the output is gone. The fix is to send it to a file yourself:
MAILTO=""
30 2 * * * /home/you/bin/job.sh >> /home/you/logs/job.log 2>&1Copied>> appends, and 2>&1 sends errors to the same file, which is where “command not found” will be when the job fails. To learn whether a job ran at all, read cron’s own log, which on Debian and Ubuntu goes to the journal under the cron unit:
journalctl -u cron --since todayCopiedBy default it records only that each job started. To also log when jobs end and which ones exit non-zero, set EXTRA_OPTS="-L 7" in /etc/default/cron and restart the service (sudo systemctl restart cron); the levels add up, 1 for starts, 2 for ends and 4 for failures. If you want the mail itself, the server needs a way to send it, and most cloud providers block port 25, some 465 and 587 as well; notifications from an agent on a server covers which are blocked and routes that avoid mail entirely.
Works in the terminal, fails in cron
Your terminal runs your login shell with everything your startup files set up. Cron runs each line with sh -c, in your home folder, with almost nothing else. From the Debian and Ubuntu manual and source, a job gets sh as its shell, HOME and LOGNAME from your account, and, unless the crontab sets one, PATH=/usr/bin:/bin. Five things follow.
- The command is not found. Anything in
/usr/local/bin,~/.local/bin, a version manager’s folder or an npm global folder is not on that PATH. Use full paths (command -v claudeprints one), or set PATH in a script. APATH=line in the crontab works too, with a catch: the manual says the value “is not parsed for environmental substitutions or replacement of variables or tilde(~) expansion”, soPATH=$HOME/bin:$PATHis taken literally. Write the folders out in full. Node installed with nvm is the common case; nvm on a server has the lines for it. - Your shell setup is not read.
.bashrc,.profileand.zshrcare not loaded, so aliases, functions and every variable exported there are missing, API keys included. In the command itself$HOMEdoes expand, becauseshruns it; only the crontab’sNAME=valuelines are literal. - It is
sh, not bash. Bash-only syntax such as[[ ]],sourceor{a,b}can fail. Put anything longer than one command in a script that starts with#!/bin/bash, or setSHELL=/bin/bashat the top of the crontab. - A
%breaks the line. Cron turns every unescaped%in the command into a newline and feeds everything after the first one to the command as input. Sodate +%Fmust be writtendate +\%F, and a prompt such as “raise coverage to 80%” typed into the line is cut short. Scripts are not affected, which is one more reason to keep the crontab line to a single call. - The times are the server’s, not yours. Debian and Ubuntu’s cron reads every line in the system’s time zone (
timedatectlshows it) and has no per-crontab zone. ATZ=line changes only what the commands see, not when they run, andCRON_TZ=does nothing for the schedule there.
To see the environment for yourself, add a line that runs every minute, wait for it, then delete it:
* * * * * env > /tmp/cron-env.txtCopiedAnd to test a job the way cron will run it, before waiting for the schedule, start it from an empty environment:
env -i HOME="$HOME" LOGNAME="$USER" PATH=/usr/bin:/bin SHELL=/bin/sh /bin/sh -c '/home/you/bin/job.sh'CopiedTime zones and daylight saving: where the two crons differ
Linux has two common crons, both descended from Paul Vixie’s. Debian and Ubuntu ship Vixie cron 3.0 with Debian’s patches (3.0pl1-200ubuntu1 on Ubuntu 26.04, 3.0pl1-184ubuntu2 on 24.04, 3.0pl1-197 on Debian 13). Fedora, Arch, openSUSE and Gentoo package cronie, whose latest release is 1.7.2 (April 2024). Apart from names in ranges, everything above holds for both; these do not:
| Debian and Ubuntu (Vixie cron) | cronie | |
|---|---|---|
Time zone per crontab | None; the system’s zone for every line | CRON_TZ=Europe/London at the top, and the times below it are London’s |
Random minute | No | ~, such as 10~50, picked when the crontab is read |
Check a file without installing | crontab -n (Debian 13) | crontab -T |
Daylight saving is less settled. The Debian cron(8) manual says a fixed-time job that falls in the hour skipped when clocks go forward runs soon after the change, and one in the hour repeated when they go back runs once; jobs with * in the minute or hour simply follow the new time. cronie’s own manuals disagree with each other: its cron(8) describes the same catch-up and no double run, while its crontab(5) says missing times “will never match” and repeated ones cause jobs “to be run twice.” A server kept on UTC, which has no daylight saving, meets none of this, and a job that must run exactly once a day is safest scheduled outside the hours the clocks change.
Scheduling a coding agent from cron
Claude Code, Codex and OpenCode each have a headless mode for this: claude -p, codex exec and opencode run. Everything above applies to them, plus three things a short script does not have: a run can take an hour, it needs a credential, and two runs at once can trip over each other in the same repository. Keep the crontab line to one call and put the rest in a script. Here is one for Claude Code, saved as ~/bin/nightly-agent.sh:
#!/bin/bash
set -uo pipefail
export PATH="$HOME/.local/bin:/usr/local/bin:/usr/bin:/bin"
cd "$HOME/project" || exit 1
mkdir -p "$HOME/logs"
log="$HOME/logs/nightly-$(date -u +%F).log"
echo "start $(date -u +%FT%TZ)" >> "$log"
CLAUDE_CODE_OAUTH_TOKEN="$(cat "$HOME/.config/claude/token")" \
timeout -s INT -k 2m 60m claude -p "$(cat prompts/nightly.md)" \
--permission-mode dontAsk --permission-prompts none \
--allowedTools "Edit(reports/**)" "Bash(npm test *)" \
--max-turns 40 < /dev/null >> "$log" 2>&1
status=$?
echo "end $(date -u +%FT%TZ) exit $status" >> "$log"
exit $statusCopiedMake it executable and create the log folder, which has to exist before cron can write to it, then add the line below with crontab -e, your own home in place of /home/you:
chmod 700 ~/bin/nightly-agent.sh
mkdir -p ~/logsCopiedMAILTO=""
30 2 * * * flock -n -o -E 75 /home/you/.nightly-agent.lock /home/you/bin/nightly-agent.sh >> /home/you/logs/cron.log 2>&1 || echo "exit $?" >> /home/you/logs/cron.logCopiedWhat each part is for:
- The PATH line finds
claude, which its installer puts in~/.local/bin, and also the tools the agent itself will call, since its commands inherit the same PATH. Add whatever else the job runs, such as a Node from nvm. - The credential. A sign-in kept in a file under your home does reach cron, because cron sets
HOME: on Linux, Claude Code’s/loginlives in~/.claude/.credentials.json, and Codex’s, when kept as a file, in~/.codex/auth.json. What does not reach it is a key exported in.bashrc. A login also expires, and nobody is there to renew it; the script above uses the one-year token from claude setup-token instead, kept in a file only you can read (chmod 600) and set for the one command, never written into the crontab, which any process running as you can read withcrontab -l. AnANTHROPIC_API_KEYworks the same way and, in-pmode, is always used when present, billing the API rather than your plan. - Permissions. Nobody can answer a prompt at 02:30.
--permission-mode dontAskwith an exact--allowedToolslist refuses everything else and carries on; running Claude Code headless on a schedule covers the modes, the sign-in, the plan’s limits and what a run does with long commands. timeout -s INT -k 2m 60mbounds the run. Cron has no time limit of its own, and an agent waiting on a stuck command would otherwise sit there into the next day. SIGINT lets Claude Code end its turn cleanly; a kill follows two minutes later if it has not. A run stopped this way exits 124, or 137 if it had to be killed.< /dev/nullgives the run an empty, closed input, so it never waits to read one.- The log. One file per day, with the start, the end and the exit status around the agent’s own output. A failure inside a
claude -prun, such as a missing sign-in, is printed as its result, so it lands here too, and so does Claude Code’sWarning: this sign-inline in the ten days before a login ends. flock -nstops runs overlapping. Cron starts every job on time whether or not the previous one has finished, and two agents editing one repository at once make a mess of it. With-na run that finds the lock taken exits at once instead of waiting;-E 75makes that exit status 75, so the|| echoat the end leavesexit 75incron.logfor a skipped run, next to the status of any run that failed.-okeeps the lock from passing to processes the agent starts: without it, a process the run left behind, such as a dev server the agent started, could go on holding the lock, and every later run would be skipped.
The other two agents fit the same script with their own line in place of the claude one. For Codex, OpenAI asks that the key be set only for the command that needs it. Pass the sandbox explicitly: OpenAI’s docs say codex exec runs read-only by default, but a project you have trusted can make it able to edit, so the same line behaves differently from machine to machine without --sandbox. And from Codex 0.174.0 (December 4, 2026) codex exec follows your configured approval policy: if that policy would ask a person, the run prints a warning on stderr and turns down every approval request. Setting the policy to never for the run keeps it to what the sandbox allows, without the warning:
CODEX_API_KEY="$(cat "$HOME/.config/codex-job.key")" \
timeout -s INT -k 2m 60m codex exec --sandbox workspace-write \
-c 'approval_policy="never"' \
"$(cat prompts/nightly.md)" < /dev/null >> "$log" 2>&1CopiedThe codex exec guide has its flags, its output files and a cron wrapper of its own. For OpenCode, add $HOME/.opencode/bin to the PATH line and use --standalone, so the run reads the key from its own environment rather than from a background service that may have started without it:
ANTHROPIC_API_KEY="$(cat "$HOME/.config/keys/anthropic")" \
timeout 60m opencode run --standalone --archived \
"$(cat prompts/nightly.md)" < /dev/null >> "$log" 2>&1CopiedOpenCode exits 0 even when a permission it needed was refused, so opencode run on cron or a timer shows what to look for in its output. Whichever agent it is, run the script once by hand with the env -i line above before trusting the schedule with it.
cron or a systemd timer
Cron is one line and works on any Linux server, with or without you logged in. A systemd timer takes a service file and a timer file, and in return does several things the script above has to arrange by hand:
| cron | systemd timer | |
|---|---|---|
Setup | One line | Two unit files and systemctl enable |
Runs with you logged out | Yes | A user timer needs lingering on |
A second run while the first is going | Starts it; flock prevents it | Not started: the timer leaves the running unit alone |
Run missed while the machine was off | Lost | Run at the next boot with Persistent=true |
Time zone | The system’s (cronie: CRON_TZ) | Per timer, in the calendar line |
Output | Mail, or your own redirect | The journal, kept with the unit |
Time limit | timeout in the command | TimeoutStartSec= |
For one agent run a night on a server that stays on, either does the job. A timer earns its two files when runs must not be missed after a restart, when the schedule belongs to a time zone other than the server’s, or when you want the run’s logs and failures in the journal next to everything else. The Claude Code schedule guide has a complete timer for the same job, and a user timer keeps running after you log out only with loginctl enable-linger.