The report script had run every night for two years. I ran it by hand to prove it still worked, it worked, and I went to bed. Three weeks later someone asked why the dashboard numbers stopped at the fourteenth.
Cron had been failing for nineteen days. It said so, every night, in plain English: report.sh: line 12: gnuplot: command not found. It said so to a mailbox on a server nobody had logged into since the intern left.
Your shell is not cron’s shell
When you type a command, you are standing in an environment that took years to accumulate. Your login shell read /etc/profile, then your ~/.profile or .bash_profile, and somewhere in there a PATH got built out of habits, package installs and one line you copied from a forum in 2017.
Cron reads none of that. It hands the job a tiny environment: a handful of variables, a SHELL that is usually /bin/sh regardless of what you log in with, and a PATH that is short. How short depends on the implementation and the OS. Some give you little more than /usr/bin:/bin. FreeBSD’s cron sets its own default that includes the sbin directories. Do not trust my memory or anyone else’s. Read crontab(5) on the machine that will run the job. It lists the defaults.
Mine had /usr/bin:/bin. The gnuplot binary lived in /usr/local/bin, because that is where a ports or third-party install puts things on the BSDs, and where a lot of hand-built software lands on Linux too. My interactive PATH had /usr/local/bin in it. Cron’s did not. For two years it didn’t matter, because the script called gnuplot and the one thing it needed was somehow found.
Why it worked for two years
That is the part that annoyed me for a day. It did not fail on day one. The original install put a gnuplot shim in /usr/bin, a leftover from a package someone had since upgraded. The upgrade moved the real binary to /usr/local/bin and removed the shim. Nobody touched the crontab, nobody touched the script, and the job broke on the night the package changed.
The script did not change. The ground under it did. This is the same flavour of failure as the config file that changed and nothing reloaded: everything you can see in the repo is unchanged and the system is not.
The test I should have run
Never test a cron job by running it in your terminal. You are testing your terminal. Test it in cron’s environment:
env -i /bin/sh -c '/opt/reports/report.sh'
env -i throws away the entire environment, so the child gets nothing you had. That is harsher than cron, which at least sets a few variables, but a script that survives it will survive cron. If it dies with command not found, you have found the bug in ten seconds instead of nineteen days.
To see what cron really gives you, add a temporary line like * * * * * env > /tmp/cronenv.txt, wait a minute, read the file, and delete the line. The file shows your PATH, SHELL, HOME and the rest as cron sees them. Do not leave that running. Environment dumps have a habit of containing things you don’t want in /tmp.
Fixes, in the order I’d use them
1. Absolute paths in the script. /usr/local/bin/gnuplot, not gnuplot. Ugly, immune to everything. If the binary moves again, the failure is loud and names the exact path.
2. Set PATH at the top of the script. One line, PATH=/usr/local/bin:/usr/bin:/bin, and now the script’s behaviour is written down in the script instead of inherited from whoever launched it. I prefer this to editing the crontab, because the script gets run from other places too, systemd timers excepted, since I do not run those.
3. Set PATH in the crontab. Most crons accept variable assignments at the top of the file. That works, and it is also the one people forget when they move the job to a new host. Check your cron’s man page before relying on it, since not every implementation parses the file identically.
4. Do not paper over it with bash -lc. Running the job through a login shell to pick up your profile makes it work, and also makes it depend on whatever is in that profile on that day. You’ve replaced one invisible dependency with a larger one. I have done this. I regret it every time someone adds an echo to .profile that ends up in a report.
The real failure was the mail
The command-not-found error is a nuisance. Nineteen days of silence is the incident.
Cron mails output to the job owner, or to MAILTO if set. On that host, local mail piled up in a spool nobody read. The alert existed. It was delivered. It went into a drawer. It is the cousin of the alert that fired for eight months: a signal nobody is positioned to receive is not a signal.
What I changed:
MAILTOpoints at a real address that forwards to a human, and I tested it by making a job fail on purpose.- The script writes a last-success timestamp file on its final line. A separate check complains if that file is older than 26 hours. Failure to run is detected by absence of success, not by hoping the error got read.
- The job is wrapped in
flockso a slow run cannot stack on the next one; I wrote that up in flock for cron overlap.
If your cron job emits % anywhere in its command line, that is its own trap, so check that too while you’re in there.
The grudging part
I used to sneer at people who put full paths on every command in a script. It looked like fear. It is not fear. It is a script that says what it needs and fails by name when the need is not met. I now write them that way by default, and PATH goes on line two.
Nineteen days of missing numbers, and the cost was one embarrassing meeting. It could have been invoices.
More like this in the reliability hub.
