Jeff Epler's blog

4 November 2013, 21:38 UTC

report-if-fail, replace-if-success


I run a lot of scripts from cron, and only want to hear about them when they fail. That's precisely what this script does. Just insert it at the start of your cron command, e.g.,
@hourly report-if-fail rsync-some-stuff
your script has to produce its output on stdout+stderr, and exit with 0 for success and nonzero for any failure.

Another thing I do is produce reports into static web pages, but sometimes the reports fail e.g., due to transient network errors. In these cases I want to keep the old version. That's what replace-if-success does: If the command succeeds then it replaces its target file with the command's stdout. Otherwise, it leaves the target file untouched. In any case, it returns the underlying program's exit status, so you can also combine it with report-if-fail. (report-if-fail will e-mail the stderr only in this case)

@hourly replace-if-success /var/www/stats.txt gather-some-statistics

Files currently attached to this page:

replace-if-success116 bytes
report-if-fail285 bytes

In the author's view, these short scripts, report-if-fail and replace-if-success, are too trivial to be subject to copyright. In the event that you disagree with this assessment, you may also use these works under the terms of the CC0 license.

[permalink]

All older entries
Website Copyright © 2004-2024 Jeff Epler