A cron job in Linux is a command or script that the cron daemon runs automatically on a schedule you define in a crontab file. It needs no logged-in user and no terminal open. The key boundary: cron runs on the server's own clock using its own environment, so a job that works when you type it by hand can still fail silently at 3 a.m. Most "broken cron" stories come from that gap, not from cron itself.
Think of cron as an alarm clock that never sleeps and never asks questions. You hand it five time fields and a command, it wakes up every minute, checks every user's crontab, and fires whatever matches the current time. That's the whole model. Everything else β paths, permissions, logging, time zones β is consequence.
Here are three realistic jobs people actually schedule. A nightly database dump at 02:30 that pipes mysqldump output to a compressed file in /var/backups. A weekly certificate renewal check that runs certbot renew every Monday at 04:00. A cleanup job that deletes temp files older than seven days every hour. Each is a single line. Each is also a small system, with failure modes.
If you've heard cron mentioned next to systemd timers, they solve the same problem with different trade-offs. Cron reads plain-text crontabs and works on every Linux distribution you'll meet; systemd timers live in unit files, log through the journal, and can catch up on missed runs after downtime. For a one-line recurring task on a machine that's always on, cron is simpler. For anything that needs dependency ordering or persistent logging by default, a timer is often the better tool β and both can coexist on the same box.
The Five Time Fields and the Operators Most Guides Skip
A crontab schedule line holds five fields followed by the command: minute (0β59), hour (0β23), day of month (1β31), month (1β12), and day of week (0β7, where both 0 and 7 mean Sunday). 30 2 * * * means 02:30 every day. 0 4 * * 1 means 04:00 every Monday. The asterisk is a wildcard meaning "every value."
The operators are where schedules get precise. A comma lists values: 0 9,17 * * * runs at 09:00 and 17:00. A hyphen gives a range: 0 9-17 * * 1-5 runs hourly from 09:00 through 17:00 on weekdays β note that the 17:00 run happens, so it's eight runs per weekday, not seven. A slash sets a step: */15 * * * * runs every 15 minutes, at :00, :15, :30, and :45.
The day-of-month and day-of-week fields behave differently than most people expect. When both are restricted β neither is * β cron fires when either field matches, not both. So 0 0 13 * 5 runs at midnight on the 13th of the month and every Friday. If you need "Friday the 13th only," you must test the date inside the command itself. That single rule trips up more people than any other part of cron syntax.
Two macros save typing and reduce mistakes. @daily equals 0 0 * * *, @weekly equals 0 0 * * 0, @monthly equals 0 0 1 * *, @reboot runs once when the daemon starts, and @yearly equals 0 0 1 1 *. A trailing % in a crontab command becomes a newline β a quirk that breaks date-stamped filenames like backup-%Y%m%d unless you escape it as %. And MAILTO= set at the top of the crontab disables the email cron otherwise tries to send on every output line.
How a Crontab Is Read and Which File to Edit
There are two kinds of crontabs and they live in different places. A user crontab, created with crontab -e, is stored under /var/spool/cron/crontabs/ on Debian-based systems or /var/spool/cron/ on Red Hat-family systems, and it runs as that user. The system crontab at /etc/crontab carries an extra field between the schedule and the command: the user to run as. Same for files dropped into /etc/cron.d/, which is where packages usually install their own jobs.
Never edit files under /var/spool/cron/ directly. Use crontab -e so the daemon notices the change and validates syntax on save. crontab -l lists your jobs, crontab -r deletes the whole crontab without asking for confirmation, and crontab -u username -e edits another user's crontab if you have root. Run crontab -e as root and the job runs as root β a fact worth pausing on before you schedule anything.
Distribution-built shortcut directories exist too. On Debian and Ubuntu, scripts in /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly run through run-parts, which only executes files whose names contain no dots β a script named backup.sh can sit there forever without running. Red Hat-family systems route these through anacron, which runs missed jobs after downtime, so a laptop that was off at 03:00 still gets its daily job. Ubuntu's default /etc/crontab calls run-parts directly rather than anacron, so a desktop that's off at the scheduled time skips the job entirely.
Access is controlled by /etc/cron.allow and /etc/cron.deny. If cron.allow exists, only listed users may create crontabs; if it doesn't and cron.deny lists you, you're blocked. Check which file exists on your system before assuming a permission problem is a syntax problem.
A Working Example: Schedule, Test, and Verify in Seven Steps
The reliable pattern is: write the script, run it by hand, then schedule it, then prove it fired. Skipping the proof step is how jobs sit broken for weeks.
1: Write the script to a fixed location, e.g. /usr/local/bin/db-backup.sh, with an absolute shebang like #!/bin/bash on line one. 2: Make it executable: chmod 750 /usr/local/bin/db-backup.sh. 3: Run it manually as the same user cron will use: sudo -u www-data /usr/local/bin/db-backup.sh. Fix errors here, not later. 4: Open the crontab with crontab -e (add sudo if the job must run as root) and add the line 30 2 * * * /usr/local/bin/db-backup.sh >> /var/log/db-backup.log 2>&1. 5: Confirm the entry saved with crontab -l. 6: For a quick sanity check, set the schedule to the next minute, wait, and confirm the log file gained a line β then change it back to 02:30. 7: Check the system log for the run: /var/log/syslog on Debian and Ubuntu (grep CRON /var/log/syslog), /var/log/cron on Red Hat-family systems.
If step 6 or 7 fails, check three things in order: whether the cron daemon is running (systemctl status cron on Debian/Ubuntu, systemctl status crond on Red Hat-family), whether the script's absolute path is correct, and whether the user has permission to write the log file. A job with a bad redirect target exits without a trace, which is why step 4 sends both stdout and stderr to a known file.
Why Your Job Runs by Hand but Fails Under Cron
Cron starts jobs with a minimal environment. PATH is typically /usr/bin:/bin β sometimes with /usr/sbin:/usr/bin:/sbin:/bin on Red Hat-family systems β and your shell profile is never read. So a script that finds python3 or node from your interactive shell may fail outright under cron with "command not found," and the only evidence is an email or a log line nobody reads.
Set what you need at the top of the crontab, not inside the script, so the fix is visible where the schedule is. PATH=/usr/local/bin:/usr/bin:/bin, SHELL=/bin/bash, and [email protected] cover most cases. Better still, call every binary by absolute path inside the script: /usr/bin/python3, not python3. Decide the working directory explicitly too β cron starts jobs in the user's home directory, so a relative path that worked in your terminal resolves somewhere else.
The other silent killer is output. Cron emails stdout and stderr to the crontab owner if a mail system is configured; on a server without one, that output vanishes. Redirect it: append >> /var/log/jobname.log 2>&1 to every job. A job that prints an error you never see is indistinguishable from a job that never ran.
On modern systems another layer intervenes. Nearly all mainstream distributions β Ubuntu, Debian, Fedora, RHEL, Arch β run cron jobs under a PAM session with resource limits and no inherited environment by default. If a job needs a specific umask, locale, or environment variable, set it in the crontab or the script; don't assume it reaches the job from anywhere else.
Time Zones, DST, and the Clock Behavior Nobody Warns You About
Cron schedules against the system clock and whatever time zone the daemon runs in. On a server set to UTC β the common default β a job at "02:30" means 02:30 UTC, not your local time. Confirm with timedatectl; if the system clock is UTC and you wanted local time, either change the schedule arithmetic or set CRON_TZ=America/New_York at the top of the crontab (supported by vixie-cron derivatives and cronie, which Red Hat-family systems use).
Candidate next-search question: what happens at daylight saving? The answer depends on the daemon and is worth knowing before you schedule anything at 02:30. On the spring-forward night, jobs scheduled for the skipped hour behave inconsistently β some daemons skip them, some fire them immediately after the jump. On the fall-back night, a job in the repeated hour can run twice. Neither outcome is a bug in your script, and both are documented in the man pages for cron and crontab on your distribution; check them rather than relying on internet folklore.
If a job must run exactly once per day no matter what happened to the clock, anacron is the right tool on workstations and laptops. It tracks last-run timestamps and catches missed daily, weekly, or monthly jobs within a configured window after boot. Cron has no equivalent for arbitrary schedules.
Here's a distinction worth internalizing: cron is a scheduler, not a job queue. If a job takes longer than its interval, the next run starts anyway, potentially overlapping the first. Add a lock β flock -n /var/lock/db-backup.lock /usr/local/bin/db-backup.sh β and the job simply skips when the previous run is still going. Without a lock, two copies writing the same file is a corruption waiting to happen.
Cron vs. Systemd Timers: A Row-by-Row Comparison for Real Jobs
Cron's age shows in two places: logging is indirect, and missed runs are lost forever. Systemd timers, present on every mainstream distribution since roughly 2015, fix both. The comparison below is the one that should decide the pick for a given job, not general preference.
| Dimension | Cron | Systemd timer |
|---|---|---|
| Definition format | Plain-text crontab | .timer plus .service unit files |
| Minimum interval | One minute | Sub-second (OnCalendar=*:0/5 is five seconds) |
| Logging | Output emailed or redirected by hand | Automatic capture in the journal (journalctl -u name) |
| Missed run after downtime | Lost | Catch-up via Persistent=true |
| Run as another user | Extra user field in system crontab files | User= in the service unit |
| Dependencies and ordering | None | Full systemd ordering and Requires= |
| Random jitter | Manual arithmetic | Built-in RandomizedDelaySec= |
| Portability | Every Unix-like system | Linux with systemd only |
The choice splits cleanly. A single-file backup script on a server that's always on? Cron. A certificate renewal that must log reliably and survive a reboot storm? A timer with Persistent=true. A task needing sub-minute frequency or strict ordering behind a database service? Timer. Anyone maintaining a mixed estate, including BSD or containers without systemd, should default to cron for portability and reach for timers only where the missing features hurt.
One more honest limitation on the cron side: the daemon reads crontabs once and caches notification of changes, so a file edited directly under the spool directory may not take effect until the daemon reloads. That's another reason crontab -e is the only sane editing method. If jobs mysteriously stop firing after a manual edit, reload the daemon as your first diagnostic step.
Debugging and Logs: Finding the Job That Never Fired
Start with the daemon's opinion of events. grep CRON /var/log/syslog on Debian and Ubuntu, or grep CRON /var/log/cron on Red Hat-family systems, shows each job cron attempted to launch with its user and command. If the line is absent, cron never ran the job β the schedule, the crontab ownership, or the daemon state is wrong. If the line is present but the effect is missing, the job launched and its own environment or script is at fault.
Walk the evidence in order: does crontab -l show the entry under the right user, does the daemon show active in systemctl status, and does the log show a launch line at the expected time. Then check the job's own redirected log, which should have a timestamp on every run. A log file with no new line is proof of a launch failure; a log file with an error is proof the script ran and misfired.
Test the exact command cron will run by wrapping it in the cron environment: env -i PATH=/usr/bin:/bin SHELL=/bin/sh /bin/sh -c 'your command here'. If it fails there, it will fail under cron, and now you can see the error. This one command catches the majority of path and environment bugs before you've spent an evening guessing.
Two false alarms deserve mentions. First, /etc/cron.d files require the user field between schedule and command β a file copied from a user crontab silently fails without it, and some daemons reject it quietly. Second, a script on a filesystem mounted noexec won't run regardless of permissions, common in hardened /tmp setups; move the script to /usr/local/bin and the problem disappears. When nothing explains the silence, check the daemon's own messages in journalctl -u cron β they often name the exact crontab line that was rejected.
Hardening Cron: Permissions, Output Control, and the Root Trap
Cron's power is proportional to the privilege of the user whose crontab holds a job. A root crontab runs root commands with root's file access β a single overwritten path in a backup script can delete system files. Write the script so every path is absolute, test it with the non-destructive flags first, and comment out the destructive line in the crontab until the log shows the expected effect.
User-crontab access should be deliberate. If /etc/cron.allow exists and lists only the operators who need jobs, everyone else β including a compromised web app user β cannot add schedules. This is a cheap, real control that most guides skip, and it's supported by every cron implementation you'll meet. Pair it with crontab -r discipline: it deletes without confirmation, so back up crontabs you care about (crontab -l > ~/crontab.bak).
Keep output on a leash. MAILTO= at the top of the crontab stops cron's automatic mail if you'd rather have files, and every line should redirect to a log that you rotate. An unrotated log from a job that prints a line every minute fills the disk in weeks, and a full disk stops every other job silently. Logrotate's default package config handles files under /var/log if you name them consistently.
Treat secret-bearing commands carefully. A crontab is world-readable on most systems, so a password pasted into the command line is effectively published to every user on the box. Put the credential in a file readable only by the job's user and have the script read it β and give the crontab file itself the tightest permissions the daemon allows. The same goes for @reboot jobs: they fire once per daemon start, not once per boot, and a container that restarts the daemon repeatedly will run them repeatedly.
FAQ
Can a cron job run every 30 seconds?
No β standard cron's smallest unit is one minute. The conventional workaround is two lines offset by 30 seconds, but the cleaner answer on systemd systems is a timer with OnUnitActiveSec=30, or OnCalendar=::0/30. If you must stay on cron, have the command itself sleep for 30 seconds and run the task, so one scheduled line produces two executions. Verify the overlap risk with a lock before deploying.
Why does my cron job work when I run it but fail on schedule?
Cron's environment is minimal β PATH is usually /usr/bin:/bin β and the working directory is the user's home, not your project directory. Most failures are a binary found via ~/.bashrc or a relative path that only resolves in your terminal. Run the command with env -i PATH=/usr/bin:/bin /bin/sh -c 'command' to reproduce the failure; the error message then tells you what to fix.
Does cron run missed jobs after the machine was off?
Not by default β a missed run is lost, and that's true of every standard cron implementation. anacron is the traditional remedy on desktops: it reads last-run timestamps and executes daily, weekly, or monthly jobs within a window after boot. With systemd, Persistent=true in a .timer file gives the same catch-up behavior for anything from hourly to yearly. Always-on servers don't notice the difference; laptops and containers do.
What is the difference between a cron job and a cron daemon?
The daemon (cron or crond) is the always-running process that checks schedules once per minute; the job is the command and its schedule in a crontab. The job cannot exist without the daemon, and stopping the daemon stops every job on the machine. Check the daemon with systemctl status cron or systemctl status crond depending on your distribution.
Can I schedule a job to run strictly every 15 minutes?
*/15 * * * * gives runs at :00, :15, :30, and :45 β four per hour, ninety-six per day β which is the closest cron gets to "every 15 minutes." It is not a rolling 15-minute gap; the intervals are pinned to the clock. If you need a rolling interval anchored to job start, or a duration of seconds, use a systemd timer with OnUnitActiveSec=.
How do I stop cron from emailing me on every run?
Set MAILTO="" at the top of the crontab, which disables mail for that crontab entirely. Or redirect each line: >> /var/log/jobname.log 2>&1. The second approach is safer, because it preserves evidence for debugging while keeping the mailbox quiet. Blanket-disabling mail without redirecting output is how jobs fail invisibly for months.
Does cron run scripts from /etc/cron.daily on my laptop?
Only if the machine is on at the time the router triggers them, and Ubuntu's default /etc/crontab calls run-parts for those directories directly β so a laptop asleep at 06:25 skips that day entirely. Red Hat-family systems route the same directories through anacron, which catches up after boot. Check which mechanism your distribution uses before relying on these directories for backups or anything that must run daily.