/
cronlog
executable file
·142 lines (115 loc) · 3.34 KB
/
cronlog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#! /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