Why cron jobs fail silently, and how to catch each kind
On this page
- 1. The output went to a mailbox nobody reads
- 2. PATH and the environment aren't your shell's
- 3. A % in the command
- 4. The job hangs
- 5. It exited 0 and did nothing
- 6. The server, the container or the crontab is gone
- The check that catches all six: report from the job
- FAQ
- Start with the job you'd least like to find broken
Cron jobs fail silently because cron only starts the command and mails whatever it prints. It never checks whether the job worked, never notices a job that hangs, and can't report anything when the server itself is down. You catch all of these the same way: the job reports its exit code to an outside monitor on every run, and the monitor alerts you on a failure code or when the report doesn't arrive on time.
Cron has a small job description. At the right minute it starts your command with /bin/sh. If the command prints anything, cron tries to mail it to you. That's it. It doesn't check that the job worked, it doesn't notice when a job never ends, and it can't tell you about a server that's gone.
So when a backup or a report quietly stops, cron usually has nothing to say. Here are the six ways that happens, how to check each one on your box today, and the one check that catches all of them next time.
1. The output went to a mailbox nobody reads
Cron mails a job's output to the crontab's owner, or to the address in MAILTO. If MAILTO="" is set, no mail is sent at all. On a lot of servers there's no mail transfer agent installed, so the "mail" goes nowhere.
Check it now: look for MAILTO at the top of crontab -l, and check whether a mailer is installed (command -v sendmail). If you want to keep the output, send it to a file in the crontab line itself:
0 3 * * * /opt/jobs/backup.sh >> /var/log/backup.log 2>&1
2. PATH and the environment aren't your shell's
Cron sets only a few variables: SHELL=/bin/sh, plus LOGNAME and HOME from the owner's account. On Debian the job's PATH is just /usr/bin:/bin. A command that works in your terminal can fail under cron because a binary lives in /usr/local/bin, or because an API key you exported in .bashrc isn't there.
Check it now: run the exact crontab command with an almost empty environment:
env -i PATH=/usr/bin:/bin /bin/sh -c '/opt/jobs/backup.sh'
If it fails here, it fails under cron. Use full paths, or set PATH= at the top of the crontab.
3. A % in the command
In a crontab line, an unescaped % ends the command. Everything after the first % is passed to the command as standard input. The classic victim is a date in a filename:
# Broken: cron cuts the command at the first % 0 3 * * * pg_dump mydb > /backups/db-$(date +%F).sql # Works: escape each % with a backslash 0 3 * * * pg_dump mydb > /backups/db-$(date +\%F).sql
Check it now: crontab -l | grep '%'. Every % should be escaped, or moved into a script.
4. The job hangs
A job that waits forever on a network call or a lock never exits, so it never fails. Nothing is printed, nothing is mailed, and tomorrow's run may pile up behind it.
Check it now: ps -eo pid,etime,cmd | grep backup.sh shows how long a running copy has been going. For the fix, wrap the command in timeout so a hang becomes a failure, and see how to detect a job that started and never finished.
5. It exited 0 and did nothing
A script can finish "successfully" after doing nothing useful: a query that matched no rows, an upload to the wrong bucket, a step that caught an error and carried on. Exit code 0 only means the last command didn't complain.
Check it now: look at the result, not the log. Is today's backup file there, and is it a sensible size? Then make the script fail loudly: start it with set -eu, and exit non-zero when the result is missing.
6. The server, the container or the crontab is gone
If the machine is down, the container was rebuilt without cron, or someone replaced the crontab, nothing runs and nothing complains. On Debian, files in /etc/cron.daily and friends are run by run-parts, which skips any file name with a dot in it, so backup.sh in /etc/cron.daily never runs.
Check it now: crontab -l on the box you think runs the job, and run-parts --test /etc/cron.daily to list what will actually run there. Logs help with "did it start": see how to check if a cron job ran.
The check that catches all six: report from the job
Every case above has one thing in common: the job doesn't tell anyone how it went. Turn that around. Have the job report to something outside the server after every run, and get alerted when the report says it failed, or when no report arrives on time.
With Stallbell, that's one extra command on the crontab line. Create a monitor with the same schedule as the job, then add:
0 3 * * * /opt/jobs/backup.sh >> /var/log/backup.log 2>&1; curl -fsS -m 10 --retry 3 https://stallbell.com/p/TOKEN/$?
$? is the job's exit code. /0 counts as a success and any other code from 1 to 255 as a failure, so Stallbell sends a Failed run alert on the first failure by default. If nothing arrives by the scheduled time plus the grace period (5 minutes unless you change it), you get a Missed run alert instead. That covers a dead server, a deleted crontab and a cron daemon that isn't running.
Which case each check covers:
| Silent failure | Exit code in the ping | Missed run | Stuck run (start ping) | Output check |
|---|---|---|---|---|
| 1 No mailer | yes | |||
| 2 PATH or environment | yes | |||
3 % in the command | yes (the ping after the % is cut off too) | |||
| 4 Hangs | with timeout | yes, if the run blocks the next ping | yes | |
| 5 Exit 0, did nothing | if your script exits non-zero | if you send output | ||
| 6 Server or crontab gone | yes |
For hangs, send a start ping too, and Stallbell alerts when a run passes its Stuck after time. The cron monitoring guide has a small wrapper that does start, finish and exit code in one line.
Alerts go by email, Slack or a signed webhook, and you get a message when the job recovers. Plans and limits are on the pricing page.
FAQ
Why doesn't cron tell me when a job fails? Cron only starts commands. It mails any output if mail is set up, but it doesn't look at the exit code, so a job that fails without printing anything leaves no trace.
Where are cron's own logs? On Debian and Ubuntu, cron logs to syslog: grep CRON /var/log/syslog, or journalctl -u cron on systemd. By default it logs when a job starts, not how it ended. See how to check if a cron job ran.
Is || curl ... enough? It catches a failing job, but not a job that never runs or never ends. A missed-run check needs something outside the server that expects the ping.
Will a monitoring outage break my job? No. The ping runs after your job, and -m 10 limits curl to 10 seconds. If the ping fails, the job's own result is unchanged.
What does Stallbell read from my server? Nothing. It only knows what the job sends in the ping: the exit code in the URL, and any fields you add.
Start with the job you'd least like to find broken
Pick the cron job you'd least like to discover broken a week late, give it a ping, and see the first check-in arrive.
Related: How to monitor cron jobs · Check if a cron job ran · Detect stuck jobs · What is heartbeat monitoring?