ts: disable utf8 warnings
[moreutils.git] / chronic
blobf70091a75952981a4585a46cac6f1c73d37f0c70
1 #!/usr/bin/perl
3 =head1 NAME
5 chronic - runs a command quietly unless it fails
7 =head1 SYNOPSIS
9 chronic COMMAND...
11 =head1 DESCRIPTION
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.
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.
23 0 1 * * * chronic backup # instead of backup >/dev/null 2>&1
25 =head1 AUTHOR
27 Copyright 2010 by Joey Hess <id@joeyh.name>
29 Original concept and "chronic" name by Chuck Houpt.
31 Licensed under the GNU GPL version 2 or higher.
33 =cut
35 use warnings;
36 use strict;
37 use IPC::Run qw( start pump finish timeout );
39 if (! @ARGV) {
40 die "usage: chronic COMMAND...\n";
43 my ($out, $err);
44 my $h = IPC::Run::start \@ARGV, \*STDIN, \$out, \$err;
45 $h->finish;
46 my $ret=$h->full_result;
48 if ($ret >> 8) { # child failed
49 showout();
50 exit ($ret >> 8);
52 elsif ($ret != 0) { # child killed by signal
53 showout();
54 exit 1;
56 else {
57 exit 0;
60 sub showout {
61 print STDOUT $out;
62 print STDERR $err;