Permalink
Switch branches/tags
Nothing to show
Find file
b74ac51 Aug 29, 2012
@kazuho @tyru
executable file 143 lines (115 sloc) 3.34 KB
#! /usr/bin/perl
use strict;
use warnings;
use Fcntl qw(SEEK_SET);
use File::Temp qw(tempfile);
use Getopt::Long;
use Pod::Usage;
our $VERSION = '0.01';
my ($logfile, $opt_timestamp, $opt_print_always, $opt_help, $opt_version);
GetOptions(
'logfile=s' => \$logfile,
'timestamp' => \$opt_timestamp,
'print-always' => \$opt_print_always,
'help' => \$opt_help,
'version' => \$opt_version,
) or pod2usage(1);
if ($opt_help) {
pod2usage(0);
} elsif ($opt_version) {
print "$VERSION\n";
exit 0;
}
die "no args"
unless @ARGV;
# open logfile
my $logfh;
if ($logfile) {
open $logfh, '>>', $logfile
or die "failed to open file:$logfile:$!";
} else {
($logfh, $logfile) = tempfile(UNLINK => 1);
}
autoflush $logfh 1;
print $logfh '-'x78, "\n";
my $logpos = tell $logfh;
die "failed to obtain position of logfile:$!"
if $logpos == -1;
seek $logfh, $logpos, SEEK_SET
or die "cannot seek within logfile:$!";
pipe my $logrh, my $logwh
or die "failed to create pipe:$!";
# exec
_log(
do { my $h = `hostname 2> /dev/null`; chomp $h; $h }
. ' starting: ' . join(' ', @ARGV) . "\n",
);
my $exit_code = -1;
unless (my $pid = fork) {
if (defined $pid) {
# child process
close $logrh;
close $logfh;
open STDERR, '>&', $logwh
or die "failed to redirect STDERR to logfile";
open STDOUT, '>&', $logwh
or die "failed to redirect STDOUT to logfile";
close $logwh;
exec @ARGV;
die "exec(2) failed:$!:@ARGV";
} else {
close $logrh;
close $logwh;
print $logfh, "fork(2) failed:$!\n"
unless defined $pid;
}
} else {
close $logwh;
_log($_) while <$logrh>;
close $logrh;
while (wait == -1) {}
$exit_code = $?;
}
# end
if ($exit_code == -1) {
_log("failed to execute command:$!\n");
} elsif ($exit_code & 127) {
_log("command died with signal:" . ($exit_code & 127) . "\n");
} else {
_log("command exited with code:" . ($exit_code >> 8) ."\n");
}
# print log to stdout
if ($exit_code != 0 || $opt_print_always) {
open my $fh, '<', $logfile
or die "failed to open $logfile:$!";
seek $fh, $logpos, SEEK_SET
or die "failed to seek to the appropriate position in logfile:$!";
while (my @l = <$fh>) {
print @l;
}
close $fh;
}
exit($exit_code >> 8);
sub _log {
my ($line, $timestamp) = @_;
print $logfh (
($timestamp || $opt_timestamp ? '[' . scalar(localtime) . '] ' : ''),
$line,
);
}
__END__
=head1 NAME
cronlog - log output of a cron job, and send mail on error
=head1 SYNOPSIS
cronlog [--logfile=logfile] [--timestamp] [--print-always] -- cmd args...
=head1 DESCRIPTION
Cronlog is a wrapper script for running cron tasks. It does the following:
=over 4
=item logs the output of the command
If logfile is specified, stdout and stderr of the command will be logged to the file so that it could be used for later inspection. If not specified, the outputs will not be logged.
=item prints the output on error
Cronlog prints the outputs the command if and only if the command execution failed (i.e. fails to start, or returns a non-zero exit code, or killed by a signal). In other words, this behaviour causes L<cron> to send mail when and only when an error occurs.
=back
=head1 AUTHOR
Kazuho Oku
=cut