Sub topic 1
Why scheduled jobs fail without anyone noticing
A scheduled job that fails loudly gets fixed within a day. The jobs that cause real damage are the ones that fail quietly for weeks. This chapter explains how that happens and sets up the rest of the tutorial. It also introduces one idea that runs through every later chapter: you have to detect a job that did not run, not only a job that ran badly.
Prerequisites
This chapter assumes you can:
- Work on the Linux command line and edit files with a terminal editor.
- Write basic shell scripts, including variables, pipes, and exit codes (
$?). - Explain what a user, a file permission, and a process are.
- Read log files and filter them with tools such as
grepandjournalctl.
You do not need prior experience with cron or systemd timers. The next chapters introduce both.
What "silent failure" means
A scheduler such as cron starts a program at a given time. It does not know what the program is for. It starts the program, waits for it to end, and moves on. Two things follow from this design:
- The scheduler considers its work done once the program has started. Whether the program accomplished anything is not its concern.
- Nobody is watching a terminal. The usual feedback you get when you run a command by hand (error text, a prompt, a visibly stuck process) goes nowhere useful.
Silent failure is the gap between "the scheduler started the job" and "the job did what it was supposed to do". Most scheduled-job incidents live in that gap.
The main ways jobs fail quietly
The environment differs from your shell
You test a script in your interactive shell, and it works. Cron runs it with a much smaller environment: a short default PATH, no login profile, no variables from .bashrc, and a different working directory. A tool in /usr/local/bin or a version manager directory is simply not found.
You can approximate this yourself:
env -i PATH=/usr/bin:/bin /bin/sh -c 'command -v aws || echo "aws not found"'env -i clears the environment, then the command sets only the PATH cron would typically provide. If your tool lives elsewhere, the job fails under cron even though it works for you. The chapter on cron's environment covers the fixes.
Output goes nowhere
Cron collects whatever a job prints. By default it tries to send that output by local mail. On many servers no mail system is installed or configured, so the error message is discarded. The job fails, writes a clear explanation, and the explanation vanishes. Capturing output deliberately is the subject of a later chapter.
The exit status is wrong, or ignored
Every process ends with an exit status. Zero means success, and anything else means failure. A shell pipeline reports only the status of its last command unless you ask otherwise. The following line shows the trap:
false | gzip > /dev/null; echo "exit status: $?"
bash -c 'set -o pipefail; false | gzip > /dev/null; echo "exit status: $?"'The first command prints exit status: 0 even though false failed. The second prints exit status: 1. A backup script written as pg_dump mydb | gzip > db.sql.gz can therefore fail at the dump step and still report success. The scheduler sees a clean exit and logs nothing unusual.
The job writes bad data and reports success
A job can crash halfway through writing its output. The next reader then finds a truncated file that looks like a normal backup. Writing to a temporary file and renaming it avoids this, and a later chapter on idempotency shows how.
Runs overlap
Suppose a job runs every 15 minutes and normally takes 2 minutes. One night the database is slow, and it takes 40 minutes. The scheduler starts a second copy while the first is still running, then a third. The copies compete for the same files, locks, and disk, which slows each of them further. Neither cron nor a plain systemd timer prevents this by itself in every setup. A lock does, as the chapter on flock explains.
The job hangs
A job that waits on a network call with no timeout may never finish. It does not fail. It just sits there, holds its lock, and blocks every later run. From the outside, nothing looks wrong, because there is no error to see.
Runs are skipped
A job scheduled for 02:30 does not run if the machine is powered off or suspended at 02:30. Plain cron does not catch up when the machine returns. The next run happens at the next scheduled time, so a daily job can silently skip a day. systemd timers with Persistent=true and anacron exist for this case.
Time behaves unexpectedly
Schedules are interpreted in a time zone. If the server's zone changes, or if the zone observes daylight saving time (DST), a job at 02:30 may run twice on one night or not at all on another, because that local time does not exist on the spring-forward night. Running servers in UTC avoids most of this.
The job runs with the wrong permissions
A job installed in the wrong user's crontab may lack read access to a file or write access to a directory. A job running as root can fail in the opposite way: it works, but does far more than it should if the script has a bug. Both cases are covered in the chapter on least privilege.
The scheduler or the job definition is broken
Sometimes the job never runs at all. A syntax error in a crontab file, a timer that was never enabled, or a stopped cron service all produce the same symptom: nothing happens, and nothing reports that nothing happened.
Failure versus absence
These causes fall into two groups, and they need different detection methods.
| Group | Examples | How you find out |
|---|---|---|
| The job ran and failed | Missing tool, non-zero exit, hang, bad output | Logs, exit status, alerts raised by the job itself |
| The job did not run | Machine off, disabled timer, broken crontab, stopped service | Only a check from outside the job, such as a heartbeat |
A job that is not running cannot report its own failure. Any alerting that depends on the job itself, such as an email sent from inside the script, is silent in exactly the cases where the job never started. This is why the final chapters add a heartbeat: the job pings a monitoring service after every success, and the service raises an alert when the pings stop.
The life of one scheduled run
The diagram shows the possible fates of a single run. Every box that is not "success and verified" needs a visible signal somewhere.
Looking at what the scheduler recorded
Even when a job is silent, the scheduler usually keeps a record that it started the job. On Debian and Ubuntu, the service is named cron:
journalctl -u cron --since "1 hour ago"On RHEL-family systems, the service is crond, and the log is typically /var/log/cron. These lines tell you that the scheduler fired and which user and command it ran. They do not tell you whether the command succeeded, and that limit is the point of this chapter. Use them to separate "never started" from "started and failed".
For systemd timers, the same question has a direct answer:
systemctl list-timers --all
systemctl status backup.serviceThe first command shows each timer's next and last activation. The second shows the last result of the service, including its exit status. Both are covered in detail in the chapter on timer units.
How the rest of the tutorial maps onto these failures
| Failure | Chapter topic |
|---|---|
| Wrong schedule or syntax | Cron syntax, systemd timers |
| Missing tools and variables | Cron's minimal environment |
| Lost output | Capturing output and logs |
| Wrong exit status | Exit status and failing loudly |
| Overlap | Preventing overlap with `flock` |
| Partial or repeated work | Idempotency and atomic writes |
| Hangs and flaky dependencies | Timeouts and bounded retries |
| Skipped runs | Persistent timers and anacron |
| Time zone surprises | Time zones and DST |
| Excess privilege | Least privilege |
| Job never ran | Heartbeat monitoring |
A habit to adopt now
Before trusting any scheduled job, ask four questions:
- How will I know it started?
- How will I know it finished successfully, and not just finished?
- How will I know if it did not run at all?
- What happens if two copies run at once, or if one run is interrupted?
If you cannot answer one of them, the job has a silent failure mode. The remaining chapters give you tools to answer each question.
Exercise
A nightly backup script runs pg_dump mydb | gzip > /var/backups/db.sql.gz with no set -o pipefail. The database is down one night. The cron log shows the job started and ended, and nobody receives an email. Name three separate reasons this failure goes unnoticed, and say which kind of failure it is (ran and failed, or did not run).
Show answer
It is a "ran and failed" case. First, the pipeline's exit status is that of gzip, which succeeds, so the scheduler and any exit-status check see success. Second, pg_dump writes its error to standard error, and with no output redirection and no working local mail, the message is discarded. Third, the script leaves a small, valid-looking but empty or truncated .gz file, so a casual check of the backup directory shows a recent file. Using set -o pipefail (or set -euo pipefail), capturing output to a log, writing to a temporary file and renaming only on success, and adding a heartbeat that fires only on success would each close one of these gaps.
Exercise
Why can a job that sends its own failure email still fail without anyone being told? Give two concrete situations.
Show answer
A job can only report problems if it starts and reaches its reporting code. If the machine was off at the scheduled time, the crontab has a syntax error, the systemd timer was never enabled, or the cron service is stopped, the job never starts and sends nothing. A hang has the same effect: the script stays stuck before it reaches the line that sends the email. Detecting these cases needs a check outside the job, such as a heartbeat monitor that alerts when expected pings stop arriving.