Marc Dierig
11 months
Stefan Langenmaier
1 year
Andreas Vöst
1 month
Claus-Theodor Riegg
4 years
Andreas Vöst
3 months
Andreas Vöst
3 months
Kim Klotz
4 months
Claus-Theodor Riegg
8 years
Andreas Vöst
6 months
Andreas Vöst
8 months

Use chronic instead of redirecting cronjob output to /dev/null

Posted . Visible to the public.

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
Emma Heinle
Last edit
Emma Heinle
License
Source code in this card is licensed under the MIT License.