GuidesRunning & maintaining

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.

December 4, 2026The Everpod team
The short answer

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 run
Copied

Each 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:

LineRuns
*/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

StringSame 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.backup
Copied

crontab -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.backup
Copied

To 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>&1
Copied

>> 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 today
Copied

By 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.

To see the environment for yourself, add a line that runs every minute, wait for it, then delete it:

* * * * * env > /tmp/cron-env.txt
Copied

And 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'
Copied

Time 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 lineCRON_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 $status
Copied

Make 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 ~/logs
Copied
MAILTO=""
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.log
Copied

What each part is for:

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>&1
Copied

The 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>&1
Copied

OpenCode 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:

cronsystemd timer
Setup
One lineTwo unit files and systemctl enable
Runs with you logged out
YesA user timer needs lingering on
A second run while the first is going
Starts it; flock prevents itNot started: the timer leaves the running unit alone
Run missed while the machine was off
LostRun 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 redirectThe journal, kept with the unit
Time limit
timeout in the commandTimeoutStartSec=

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.

Run Claude Code, Codex, OpenCode, Pi, Hermes or OpenClaw on an always-on developer pod.

A developer pod is a permanent cloud computer (VPS), set up and secured for you, with your pick of Claude Code, Codex, OpenCode, Pi, Hermes and OpenClaw installed. Run more agents at once than your laptop can hold, and keep them working when it’s closed. From $24 a month, built in about ten minutes.