| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
chronic - runs a command quietly unless it fails |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
chronic [-ev] COMMAND... |
| 10 |
|
| 11 |
=head1 DESCRIPTION |
| 12 |
|
| 13 |
chronic runs a command, and arranges for its standard out and standard |
| 14 |
error to only be displayed if the command fails (exits nonzero or crashes). |
| 15 |
If the command succeeds, any extraneous output will be hidden. |
| 16 |
|
| 17 |
A common use for chronic is for running a cron job. Rather than |
| 18 |
trying to keep the command quiet, and having to deal with mails containing |
| 19 |
accidental output when it succeeds, and not verbose enough output when it |
| 20 |
fails, you can just run it verbosely always, and use chronic to hide |
| 21 |
the successful output. |
| 22 |
|
| 23 |
0 1 * * * chronic backup # instead of backup >/dev/null 2>&1 |
| 24 |
*/20 * * * * chronic -ve my_script # verbose for debugging |
| 25 |
|
| 26 |
=head1 OPTIONS |
| 27 |
|
| 28 |
=over 4 |
| 29 |
|
| 30 |
=item -v |
| 31 |
|
| 32 |
Verbose output (distinguishes between STDOUT and STDERR, also reports RETVAL) |
| 33 |
|
| 34 |
=item -e |
| 35 |
|
| 36 |
Stderr triggering. Triggers output when stderr output length is non-zero. |
| 37 |
Without -e chronic needs non-zero return value to trigger output. |
| 38 |
|
| 39 |
In this mode, chronic's return value will be C<2> if the command's return |
| 40 |
value is C<0> but the command printed to stderr. |
| 41 |
|
| 42 |
=back |
| 43 |
|
| 44 |
=head1 AUTHOR |
| 45 |
|
| 46 |
Copyright 2010 by Joey Hess <[email protected]> |
| 47 |
|
| 48 |
Original concept and "chronic" name by Chuck Houpt. |
| 49 |
Code for verbose and stderr trigger by Tomas 'Harvie' Mudrunka 2016. |
| 50 |
|
| 51 |
Licensed under the GNU GPL version 2 or higher. |
| 52 |
|
| 53 |
=cut |
| 54 |
|
| 55 |
use warnings; |
| 56 |
use strict; |
| 57 |
use IPC::Run qw( start pump finish timeout ); |
| 58 |
use Getopt::Std; |
| 59 |
|
| 60 |
our $opt_e = 0; |
| 61 |
our $opt_v = 0; |
| 62 |
getopts('ev'); # only looks at options before the COMMAND |
| 63 |
|
| 64 |
if (! @ARGV) { |
| 65 |
die "usage: chronic COMMAND...\n"; |
| 66 |
} |
| 67 |
|
| 68 |
my ($out, $err); |
| 69 |
my $h = IPC::Run::start \@ARGV, \*STDIN, \$out, \$err; |
| 70 |
$h->finish; |
| 71 |
my $ret=$h->full_result; |
| 72 |
|
| 73 |
if ($ret >> 8) { # child failed |
| 74 |
showout(); |
| 75 |
exit ($ret >> 8); |
| 76 |
} |
| 77 |
elsif ($ret != 0) { # child killed by signal |
| 78 |
showout(); |
| 79 |
exit 1; |
| 80 |
} |
| 81 |
elsif ($opt_e && (length($err) > 0)) { |
| 82 |
showout(); |
| 83 |
exit 2; |
| 84 |
} |
| 85 |
else { |
| 86 |
exit 0; |
| 87 |
} |
| 88 |
|
| 89 |
sub showout { |
| 90 |
print "STDOUT:\n" if $opt_v; |
| 91 |
print STDOUT $out; |
| 92 |
print "\nSTDERR:\n" if $opt_v; |
| 93 |
STDOUT->flush(); |
| 94 |
print STDERR $err; |
| 95 |
STDERR->flush(); |
| 96 |
print "\nRETVAL: ".($ret >> 8)."\n" if $opt_v; |
| 97 |
} |