← all posts

Cron pipeline exits 0 when pg_dump fails: pipefail and empty backups

pg_dump | gzip in cron reported success while the database was down and left a 20-byte backup. What pipefail fixes, where /bin/sh lacks it, and what it doesn't fix.

8 min read

The nightly backup ran for weeks. The heartbeat check after it was green every morning. Then you needed the backup, and the file was 20 bytes.

The cron line was the one in half the tutorials out there:

0 3 * * * pg_dump app | gzip > /backups/app.sql.gz && curl -fsS https://hc.example.com/ping/backup

We ran this line and four variants under Debian 13 cron with a real PostgreSQL 17, first with the database up, then with it stopped. Then we ran the same pipelines under RunWisp, at the end.

debian 13 cron · postgresql 17 · then the database stopped

  • pg_dump | gzipexit 0, heartbeat fired
  • 4.4 MB backup20 bytes left after one failed run
  • pipefail on 24.04dash errors out, backup never runs
  • seq | head -n 1exit 141 with pipefail on
  • runwisp 1.10.0-e stopped at pg_dump, old backup kept

Short answer

A pipeline’s exit code is its last command’s. gzip doesn’t care that pg_dump failed. It compresses nothing and exits 0, so cron and your heartbeat see a success. Turn on pipefail so any failed command fails the pipeline, and write to a temp file so a failed run can’t replace the last good backup:

0 3 * * * set -o pipefail; pg_dump app | gzip > /backups/app.sql.gz.tmp && mv /backups/app.sql.gz.tmp /backups/app.sql.gz

Check your /bin/sh first: on Ubuntu 24.04 and Debian 12 it has no pipefail, and that line never runs the backup. Put SHELL=/bin/bash at the top of the crontab there. Or skip the pipe: pg_dump -Z 6 -f compresses on its own. RunWisp, an open-source cron replacement that ships as a single binary, keeps pg_dump’s error in the run’s log and can fail the run and alert on that error with one line of config, whatever /bin/sh you have.

What cron saw

Five lines in /etc/cron.d, every minute, on Debian 13. The first run had the database up, and all five wrote a 4.4 MB backup. Then we stopped PostgreSQL and waited for the next run:

Cron command Exit code Backup after the failed run Heartbeat fired?
pg_dump app | gzip > f.gz 0 20 bytes yes
set -o pipefail; pg_dump app | gzip > f.gz 1 20 bytes no
set -o pipefail; pg_dump app | gzip > f.gz.tmp && mv f.gz.tmp f.gz 1 4.4 MB, the old one no
pg_dump -Z 6 -f f.gz app 1 4.4 MB, the old one no
sync.sh 2>&1 | tee -a sync.log (sync.sh exits 1) 0 n/a yes

“Heartbeat” here was && date > ok at the end of each line, which is where a ping URL usually goes.

pg_dump printed its error every time:

pg_dump: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory

That’s stderr, so cron mails it to MAILTO, and with no mail set up it goes nowhere. Nothing that checks exit codes ever sees it. Cron failure alerts covers the checks themselves.

Why the pipeline lies

The shell starts every command in a pipeline at once and waits for all of them. Its exit code is the last command’s, and the others are thrown away. In bash you can still read them right after:

$ bash -c 'false | gzip > /tmp/x.gz; echo "exit $?, stages: ${PIPESTATUS[*]}"'
exit 0, stages: 1 0

set -e doesn’t help. It stops the script when a command fails, and from the shell’s point of view the pipeline didn’t: sh -e -c 'false | gzip > x.gz' exits 0. set -o pipefail changes the rule: the pipeline returns the last nonzero exit code from any of its commands, or 0 if all of them succeeded.

Does your /bin/sh have pipefail?

Cron runs every line with /bin/sh unless the crontab sets SHELL. Whether set -o pipefail works there depends on the distro:

System /bin/sh set -o pipefail
Debian 12 dash 0.5.12-2 error, the line stops
Debian 13 dash 0.5.12-12 works
Ubuntu 24.04 dash 0.5.12-6ubuntu5 error, the line stops
Ubuntu 26.04 dash 0.5.12-12ubuntu3 works
Alpine 3.22 BusyBox sh works
any bash works

The error case is worse than no pipefail at all. In dash, set with an unknown option is a fatal error, so the shell exits with code 2 and the rest of the line never runs:

sh: 1: set: Illegal option -o pipefail

We put set -o pipefail; date > /tmp/ran in a cron line on Ubuntu 24.04. It never wrote the file. A backup “fixed” like this stops running, and cron says nothing. If the heartbeat ping was on the same line, at least the heartbeat check notices it went quiet.

Where it’s missing, use bash for the crontab:

SHELL=/bin/bash
0 3 * * * set -o pipefail; pg_dump app | gzip > /backups/app.sql.gz.tmp && mv /backups/app.sql.gz.tmp /backups/app.sql.gz

SHELL applies to every line below it. Alpine doesn’t ship bash, but its BusyBox sh has pipefail anyway. For anything longer than one line, a script that starts with #!/bin/bash and set -euo pipefail doesn’t depend on cron’s shell at all.

pipefail doesn’t save the old backup

Look at row two of the test again: the job failed, as it should, and the backup was still 20 bytes.

> /backups/app.sql.gz truncates the file when the shell sets up the pipeline, before pg_dump even connects. By the time anything fails, the old backup is gone. pipefail only changes the exit code you get afterwards.

So write somewhere else and swap only on success:

pg_dump app | gzip > /backups/app.sql.gz.tmp && mv /backups/app.sql.gz.tmp /backups/app.sql.gz

With pipefail, the && stops a failed dump from reaching mv, and the last good backup stays where it was. The failed .tmp file stays too (20 bytes in our test), which is handy for a look and gets overwritten by the next run. mv within one filesystem is a rename, so nothing ever sees half a file under the real name.

Timestamped names like app-$(date +\%F).sql.gz don’t truncate an old backup, but they still leave a 20-byte file that looks like a backup, and rotation scripts that keep “the last 7” happily keep seven of those.

Or don’t use a pipe

pg_dump can compress by itself, which leaves one process and one honest exit code:

0 3 * * * pg_dump -Z 6 -f /backups/app.sql.gz app

It’s still plain SQL, gzipped, so zcat app.sql.gz | psql app restores it like before. With the database down, pg_dump failed before it opened the file, and the old backup stayed. That’s not true for every failure: when we killed a running pg_dump -Z 6 -f halfway, it left a 10.6 MB piece of a 44 MB dump under the real name. Keep the .tmp and mv here too.

mysqldump can’t compress its output, so for MySQL it’s the pipeline with pipefail, or --result-file and a separate gzip step after it succeeds.

| tee hides failures too

The same rule hits jobs that pipe into tee to keep a log:

*/10 * * * * /usr/local/bin/sync.sh 2>&1 | tee -a /var/log/sync.log

tee almost never fails, so this line always exits 0. In our test sync.sh exited 1 every run, the log said sync: error: connection refused, and the heartbeat after the line fired anyway.

Under cron there’s no terminal to show the output on, so tee buys you nothing. A redirect keeps the exit code:

*/10 * * * * /usr/local/bin/sync.sh >> /var/log/sync.log 2>&1

Where cron logs go covers the logging side.

pipefail and exit 141

Once pipefail is on, some pipelines start failing that used to work:

$ bash -c 'set -o pipefail; seq 1 1000000 | head -n 1 > /dev/null; echo $?'
141

head exits after one line. seq is still writing, so it gets SIGPIPE and dies with 128 + 13 = 141. Without pipefail you never saw it, because head exited 0. Dash on Debian 13 gave the same 141.

Whether it’s a failure depends on the job. “Give me the first line” is fine. A backup or an export cut short is not. If the early stop is intended, turn pipefail off for that one pipeline:

first=$(set +o pipefail; zcat big.log.gz | head -n 1)

Don’t add || true to the whole job or allow 141 everywhere. That hides the next real failure along with it.

Pipelines in RunWisp

Under cron, the failed backup left nothing behind: the error went to a mail nobody received, and the heartbeat stayed green.

RunWisp is an open-source cron replacement that ships as a single binary. We ran the same plain pg_dump | gzip on RunWisp 1.10.0 and stopped the database. The pipeline still exits 0, but the run keeps what happened:

pg_dump: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
        Is the server running locally and accepting connections on that socket?
[INFO] run succeeded task=backup run=01M84QNCZKC9T7HR5W048E6C5G exit=0 dur=13ms

The error is in that night’s log, and the run history shows a backup that normally takes 2.1 seconds finishing in 13 ms. One look at the history the next morning shows it, not the day you need the backup.

To make the run fail, drop the pipe and use two lines:

[tasks.backup]
cron = "0 3 * * *"
user = "postgres"
run  = """
pg_dump -Z 6 -f /backups/app.sql.gz.tmp app
mv /backups/app.sql.gz.tmp /backups/app.sql.gz
"""

RunWisp runs every task with -e, so the script stops at the first line that fails. With the database down, pg_dump exited 1 and mv never ran. The run was marked failed, which is what fires the task’s notify alerts, so there’s no heartbeat service to wire up. The 44 MB backup from the night before was untouched. We ran this on Ubuntu 24.04, whose dash has no pipefail, and it didn’t need it: there’s no pipe, and no && chain either.

For a script you can’t change, say a vendor backup tool that pipes inside and always exits 0, a pattern on the task fails the run when the error shows up in the output, whatever the exit code:

failures = ["+output:^pg_dump: error"]
Task, database stopped Recorded as Exit code Old backup
plain pg_dump | gzip, like the cron line succeeded 0 20 bytes
pg_dump -Z 6 -f to .tmp, then mv failed 1 kept

Common questions

Why does my cron job exit 0 when pg_dump fails?
A pipeline's exit code is the exit code of its last command. In pg_dump app | gzip > app.sql.gz, gzip gets empty input and writes a valid 20-byte gzip file. It exits 0, so the whole line succeeds. Turn on pipefail (set -o pipefail) so the pipeline fails when any command in it does, or avoid the pipe with pg_dump -Z 6 -f app.sql.gz app.
Does set -e catch a failure inside a pipeline?
No. set -e stops the script when a command fails, and the pipeline as a whole didn't fail: it returned gzip's 0. sh -e -c 'false | gzip > x.gz' exits 0. You need pipefail for that, and set -e on top if later lines shouldn't run.
Why does cron say set: Illegal option -o pipefail?
Cron runs commands with /bin/sh. On Ubuntu 24.04 and Debian 12 that's a dash without pipefail, so set -o pipefail is an error and the shell exits with code 2 before running anything after it. The job silently stops running. Debian 13, Ubuntu 26.04 and Alpine's BusyBox sh have it. On the older systems put SHELL=/bin/bash at the top of the crontab, or move the command into a script that starts with #!/bin/bash.
Why is my cron backup file only 20 bytes?
20 bytes is what gzip writes for empty input. The command that should have fed it (pg_dump or mysqldump) failed, and the shell had already truncated the old file when it set up the > redirect. pipefail makes the job fail but doesn't bring the old file back. Write to a temporary file and mv it over the real one only when the pipeline succeeded.
Why does my pipeline exit with 141 after I turned on pipefail?
141 is 128 + 13, a process killed by SIGPIPE. head -n 1 exits after one line, and the command still writing into the pipe gets SIGPIPE. Without pipefail the pipeline returns head's 0 and you never see it. seq 1 1000000 | head -n 1 returned 141 under pipefail in both bash and dash. If the early stop is intended, run that one pipeline without pipefail instead of adding || true to the whole job.
Is there a cron replacement that catches a failed pg_dump in a pipeline?
RunWisp, an open-source cron replacement that ships as a single binary, keeps every run's output, so pg_dump's error sits in that run's log instead of a cron mail that went nowhere. One line on the task, failures = ["+output:^pg_dump: error"], marks the run failed and sends an alert when that error appears, even though the pipeline exited 0. It works on any /bin/sh, including the Ubuntu 24.04 dash that has no pipefail. Tasks run with -e, so pg_dump -Z 6 -f to a .tmp file followed by mv on the next line fails the run and keeps the old backup when the dump fails, with no pipefail needed.
  1. Retry a failed cron job automatically, without hiding the failure

    The retry loop everyone posts runs once under cron's /bin/sh and exits 0. A wrapper that keeps the exit code, plus what curl and systemd retries did in our tests.

  2. List all cron jobs for all users on Linux, including the ones crontab -l misses

    The usual crontab -l -u loop found 2 of the 9 cron lines on our Debian 13 test box. Where cron jobs actually live on each distro, a script that lists every one with its owner, and the crontab userdel -r leaves behind.

Get started
GPL 3.0 · no accounts · free to self-host

Start in 30 seconds. Ship on your terms.

A single Go binary. If it doesn't earn the disk space, rm it. No account, no signup, no catch.

Install in 30 seconds
curl -fsSL https://get.runwisp.com | sh