{"canonical_url":"https://engineer.company/glossary/cron/","content_markdown":"# cron\n\n## What it is\n\ncron is the Unix service that starts commands at set times, with nobody logged\nin to start them. Each user's schedule is kept in a crontab: a plain text table\nwith one job per line. The same word names the `crontab` command, which\ninstalls, prints and deletes that table.\n\nPOSIX standardises the command and the table format, but not the program that\nreads the tables. Its rationale says so plainly: “All references to a cron\ndaemon and to cron files have been omitted.” Which daemon runs the jobs is left\nto each system. The cron manual pages this page draws on come from cronie, whose\ncrontab(5) names Paul Vixie as author and calls its format a V7-style one with\nextensions that stay compatible.\n\nA line has six fields separated by blanks. The first five say when: minute (0 to\n59), hour (0 to 23), day of the month (1 to 31), month (1 to 12) and day of the\nweek (0 to 6, where 0 is Sunday). Each field is an asterisk for every value, a\nnumber, a hyphen range such as `1-5`, or a comma list of those. The sixth field\nis the command, which `sh` runs. That is where the standard stops. Steps such as\n`*/15`, three-letter names such as `mon`, a `7` for Sunday, a username column in\nsystem tables and shorthands such as `@daily` are cronie extensions; a table\nthat uses them is no longer portable.\n\n`crontab -e` opens your table in an editor, `crontab -l` prints it and\n`crontab -r` removes it, while `crontab file` replaces it with the contents of a\nfile. cronie also keeps system tables in `/etc/crontab` and `/etc/cron.d/`,\nwhere an extra field between the times and the command names the user to run as.\nPOSIX expected as much: its rationale notes that BSD-derived systems are likely\nto append a user field.\n\n## Why it matters\n\nMuch of the upkeep of a server happens while nobody is watching: old files\ncleared, reports mailed, databases dumped, backups taken. cron is the standard\nway to say “run this at three in the morning”, and the utility has\nbeen in the standard since Issue 2, as the change history of its POSIX page\nrecords. A table that sticks to the POSIX fields reads the same on any\nconforming system.\n\nThe weakness is built in. A scheduled job has no audience, so when it breaks, no\nscreen turns red. POSIX says that output and errors a command leaves\nunredirected are mailed to the user by a method each implementation chooses. On\na host where local mail goes nowhere, that is where the error goes too. Most of\nthe trouble with cron traces back to this silence.\n\nTwo neighbours now share the work. On Linux systems run by systemd, a timer unit\ncan start the same job and adds what cron lacks: it can catch up on a run missed\nwhile the machine was off, spread start times at random, and it never starts a\nsecond copy of a service that is still running. Inside PostgreSQL, the `pg_cron`\nextension reads the same five-field schedules, and a background worker in the\ndatabase keeps track of the jobs. Our\n[River job-queue project](/portfolio/moved-slow-work-onto-a-river-job-queue/) is\nan example: in October 2026 its page counts 20 `pg_cron` jobs beneath the queue,\nwhich prune partitions, rotate salts and refresh aggregates, housekeeping the\npage says belongs in the database.\n\n## What it looks like in practice\n\nTake two jobs: a weekday clean-up and a nightly database dump. The table sets\nits own environment first, because a job does not inherit the settings of your\nlogin shell. The `NAME=value` lines and the `MAILTO` variable are cronie\nfeatures, not POSIX ones.\n\n```text\nSHELL=/bin/sh\nPATH=/usr/local/bin:/usr/bin:/bin\nMAILTO=ops@example.com\n# min hour dom mon dow command\n15 3 * * 1-5 find \"$HOME\" -name core -exec rm -f {} +\n30 2 * * * flock -n /var/lock/dump-db dump-db \u003e\u003e \"$HOME/dump.log\" 2\u003e\u00261\n0 * * * * date +\\%s \u003e\u003e \"$HOME/hourly.log\"\n```\n\nRead the first job as “at 03:15, Monday to Friday”. The second runs every night\nat 02:30, writes its output and errors to a log, and is wrapped in `flock -n`,\nwhich gives up at once instead of waiting when the lock is already held, so a\ndump that runs long never has a twin. The third shows the escaped percent sign:\nwithout the backslash, cron would cut the command at `%`.\n\nKeep the table in a file under version control and install it whole, rather than\nediting it live:\n\n```sh\ncrontab jobs.cron\ncrontab -l\n```\n\nThe second command prints what cron will actually read. cronie's `-T` option\nchecks a file's syntax before you install it, and the file must end with a\nnewline.\n\nThe same nightly dump as a systemd timer takes two units. The service says what\nto run, the timer says when:\n\n```ini\n# /etc/systemd/system/dump-db.service\n[Service]\nType=oneshot\nExecStart=/usr/local/bin/dump-db\n\n# /etc/systemd/system/dump-db.timer\n[Timer]\nOnCalendar=*-*-* 02:30:00\nPersistent=true\nRandomizedDelaySec=10min\n\n[Install]\nWantedBy=timers.target\n```\n\n`Persistent=true` stores the time of the last run on disk and starts a missed\nrun as soon as the timer comes back. `RandomizedDelaySec=` adds a random delay\nof up to ten minutes, so a fleet of machines with the same file does not hit the\ndatabase server in the same second. `systemd-analyze calendar '*-*-* 02:30:00'`\nprints the normalised expression and when it will next fire, which is the\nquickest way to test a schedule before trusting it.\n\nA schedule says when a job starts, not whether it did its work. On a 464 MB host\nof ours, the\n[dead-man's switch](/portfolio/built-dead-mans-switch-host-monitoring/) turns\nthat round: a timer starts a script four times an hour, with jitter, and the\nscript pings an outside service only when neither memory nor disk has fallen\nbelow its threshold. When the pings stop, the alarm goes off.\n\n## Where it goes wrong\n\n**The command works in your shell and not in cron.** POSIX promises a job only\n`HOME`, `LOGNAME`, `PATH` and `SHELL`, and the `PATH` need only find the\nstandard utilities. Nothing from your profile is loaded. The fix is to use\nabsolute paths or set `PATH` at the top of the table, and to test the job with\nthat same sparse environment.\n\n**Errors vanish.** Unredirected output is mailed. cronie sends it to the owner\nof the table, to the address in `MAILTO`, or nowhere at all when `MAILTO=\"\"`.\nThe fix is to redirect output to a log someone reads and, for jobs that matter,\nto have the job report success to something that complains when the reports\nstop, as the dead-man's switch above does.\n\n**A percent sign splits the command.** In the command field `%` becomes a\nnewline, only the first line runs and the rest is fed to it as standard input,\nso `date +%s` breaks. The fix is to escape it as `\\%`, or to move the command\ninto a script and call that.\n\n**Two day fields mean either, not both.** When both the day of the month and the\nday of the week are restricted, a day matching either runs the job. POSIX's own\nexample, `0 0 1,15 * 1`, fires on the 1st, the 15th and every Monday. The fix is\nto restrict only one of the two, or use a systemd calendar, where\n`Mon *-*-01..07 09:00` matches only when the weekday and the date both fit: the\nfirst Monday of the month.\n\n**Steps restart every hour.** cronie evaluates a step only within its own field,\nso `*/35` in the minutes runs at :00 and :35, not every 35 minutes, and `*/23`\nin the hours runs at midnight and 23:00. The fix is a step that divides the\nrange evenly, or a systemd timer with `OnUnitActiveSec=`, which counts from the\nlast time the service was started.\n\n**Clocks change.** cronie documents that times skipped by a daylight-saving\nchange never match, so those jobs do not run, and that repeated times make jobs\nrun twice. The fix is to schedule outside the hours your time zone shifts, or to\nset `CRON_TZ` to a zone without daylight saving.\n\n**The last line has no newline.** cronie wants every entry to end in a newline;\nwhen the final one does not, it treats the table as at least partly broken and\nwrites a warning to syslog. The fix is a trailing newline, which an editor\nsetting or a check in review can guarantee.\n\n**Runs overlap.** The crontab format has no field for what should happen when\nthe previous run is still going. A systemd timer leaves a running service alone.\nIf the timer's next scheduled time passed while the service was still running,\nthe timer starts the service again the moment it finishes, unless\n`DeferReactivation=` is set; `pg_cron` makes a second instance wait until the\nfirst one completes. The fix for plain cron is a lock, such as the `flock -n` in\nthe example above.\n\n**The machine was off.** A crontab line names a moment, and the format has no\nway to say “run it later if that moment passed”. The fix, where systemd runs the\nmachine, is `Persistent=true` on an `OnCalendar=` timer.\n\n**Every machine at once.** A table copied to a hundred hosts starts a hundred\njobs in the same minute. The fix is `RandomizedDelaySec=` on a systemd timer, or\nin cronie the `RANDOM_DELAY` variable or a tilde range such as `6~15`, which\npicks one random minute when the table is read.\n\n**A bare `crontab` wipes the table.** Typed with no argument, the command waits\nfor a new table on standard input, and an end-of-file (usually Ctrl-D) replaces\nthe current one with nothing. The fix is to press the interrupt key instead, and\nto keep the table in a file you install with `crontab file`.\n\n## Further reading\n\n- [crontab, POSIX.1-2024](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/crontab.html)\n  — the standard's six fields, default environment, mail rule and rationale.\n- [crontab(5), cronie](https://man7.org/linux/man-pages/man5/crontab.5.html)\n  — steps, `MAILTO`, `CRON_TZ`, `RANDOM_DELAY`, daylight saving and caveats.\n- [systemd.timer(5)](https://www.freedesktop.org/software/systemd/man/latest/systemd.timer.html)\n  — `OnCalendar=`, `Persistent=`, `RandomizedDelaySec=` and running services.\n- [systemd.time(7)](https://www.freedesktop.org/software/systemd/man/latest/systemd.time.html)\n  — the calendar event syntax, its shorthands and how to validate it.\n- [flock(1)](https://man7.org/linux/man-pages/man1/flock.1.html)\n  — wrapping a command in a lock, with `-n` to give up when it is held.\n- [pg_cron](https://github.com/citusdata/pg_cron)\n  — cron schedules for SQL jobs, kept inside PostgreSQL as an extension.\n\n\u003chttps://engineer.company/glossary/cron/\u003e\n","date_modified":"2026-10-29T11:17:41Z","description":"What cron is, how a crontab line is read, what POSIX leaves to the daemon, and the quiet ways a scheduled job stops doing its work.","formats":{"gemtext":"https://engineer.company/glossary/cron/index.gmi","gopher":"https://engineer.company/glossary/cron/gophermap","html":"https://engineer.company/glossary/cron/","json":"https://engineer.company/glossary/cron/index.json","markdown":"https://engineer.company/glossary/cron/index.md","pdf":"https://engineer.company/glossary/cron/index.pdf"},"kind":"page","language":"en","title":"What is cron?","url":"https://engineer.company/glossary/cron/","version":1}