When setting up cronjobs, commands somtimes output something every time. If these commands don't support limiting their output to errors only, this will create a lot of unnecessary email notifications. Often times we tend to use > /dev/null 2>&1
to just suppress all the output. This will hide errors though, and hinder debugging.
Consider chronic
from the debian package moreutils
. This will run a command quietly, unless it fails. On error code 0
(success) and with only output on STDOUT
, all the output will be discarded. On error code != 0
(error) or with output on STDERR
, everything will be returned to allow debugging. This is ideal for mail reports from cronjobs.
In your cron jobs, just prefix the actual command with chronic
.
Example
$ cat good.sh
#!/bin/bash
echo "all good!"
exit 0
$ cat bad.sh
#!/bin/bash
echo "all good until now"
echo "everything is broken" >> /dev/stderr
exit 1
$ chronic ./good.sh
$ echo $?
0
$ chronic ./bad.sh
all good until now
everything is broken
$ echo $?
1