chronic, which is part of moreutils, is a great tool for selective failure-only email notification of cron tasks:

chronic runs a command, and arranges for its standard out and standard
error to only be displayed if the command fails (exits nonzero or
crashes).  If the command succeeds, any extraneous output will be

I would like to use chronic, but with the addition of (always) appending the full stdout and stderr to a logfile.

How can I achieve this? (redirects/named pipes?)


If you are not running simultaneous commands to the same log, you can do something simple by just remembering where the current end of the log file is, then dumping it from that point on failure. Here's a test using functions instead of scripts:

    let len=1+$(stat -c %s log)
    if ! "$@" >>log 2>&1
    then    tail -c +"$len" log
            return 1

cmd()( echo stdout; echo stderr msg >&2; exit $1; )

chronicish cmd 0
chronicish cmd 1

The test command is function cmd which takes a number to use as exit, and which writes some text to stdout. The function chronicish is used to run the command and tail the logfile if it fails.

For simplicity, I've combined stdout and stderr into one log, but obviously the same technique can be done for two separate logfiles.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.