diff --git a/cronwrap/cronwrap b/cronwrap/cronwrap new file mode 100755 index 0000000..1264122 --- /dev/null +++ b/cronwrap/cronwrap @@ -0,0 +1,120 @@ +#! /usr/local/system/perl/bin/perl -w +use strict; + +use Getopt::Std; +use Mail::Mailer; + +# debug metrics are written to cronwrap.log (in cwd which should be set to ~ by cron) if it exists +# +# -t dir : use this directory to capture output of script (default: cwd ) +# -m address : mail to this address (default $LOGNAME) +# -v : do send mail - even when exist status signals success + +my $logfile="cronwrap.log"; +if (-e $logfile) +{ + open STDOUT,">>$logfile"; + open STDERR,">&STDOUT"; +} +else +{ + open STDOUT,">/dev/null"; + open STDERR,">/dev/null"; +} + +my %opt; +getopts('m:t:l:v',\%opt); +my $cmd=join " ",@ARGV; +my $start_time=localtime; +my $hostname=`/bin/hostname`; +chomp($hostname); + +warn "+++ $$ $start_time $hostname $0 $cmd\n"; + +die "no command" if $cmd eq ""; +#warn "environment:\n",map("\t$_ => $ENV{$_}\n",keys %ENV); + +my $tmpdir=$opt{'t'}||'.'; +my $tmpout="$tmpdir/cronwrap.tmp.$$"; +#warn "tempout : $tmpout\n"; + +END +{ + $tmpout && -e $tmpout && unlink $tmpout; +} + +-e $tmpout && unlink $tmpout; + +my $pid=fork; +defined $pid or die "fork failed $!\n"; +unless ($pid) +{ + my $umask=umask 077; + open STDOUT,">$tmpout"; + open STDERR,">&STDOUT"; + umask $umask; + exec {'/bin/sh'} '-sh','-c',$cmd; + die "exec failed : $!\n"; +} +wait; +my $status=$?; + +if ($tmpout && -e $tmpout && $opt{'l'}) { + if (open OUT, ">>$opt{'l'}") { + open T,"<$tmpout"; + while () { + print OUT; + } + close T; + close OUT; + } else { + warn "can't open output-logfile ('$opt{l}') to append output to: $!"; + } +} + +if ($status || $opt{v}) +{ + my $subject = ($status ? 'FAIL: ':'')."$cmd"; + my $to=$opt{'m'}||$ENV{'LOGNAME'}||"nobody"; + my @to=split/,/,$to; + @to=map {/@/ ? $_ : "$_ <$_\@molgen.mpg.de>" } @to; + + my $mailer=new Mail::Mailer 'smtp',Server=>'harry.molgen.mpg.de'; + $mailer->open + ( + { + From => "\"cronwrap\@$hostname\" ", + To => \@to, + Subject => $subject, + } + ); + + print $mailer "You cron command $cmd\n"; + print $mailer " started $start_time on $hostname\n"; + print $mailer " completed ".scalar(localtime)."\n"; + + if ($status) { + print $mailer "It terminated with an exit value of ", + $status>>8," and a signal status of ",$status&255,"\n"; + } + if (-s $tmpout) { + print $mailer "It produced the following output:\n\n"; + print $mailer "-"x60,"\n"; + open T,"<$tmpout"; + while () { + $_ eq ".\n" and $_="..\n"; + print $mailer $_; + } + print $mailer "-"x60,"\n\n"; + close T; + } else { + print $mailer "It didn't produce any output.\n"; + } + print $mailer "\n\nYours\n cronwrap.pl\n"; + $mailer->close; + warn "--- $$ ",scalar(localtime),' ',"$hostname completion status: $status - mail sent to $to\n"; +} +else +{ + warn "--- $$ ",scalar(localtime),' ',"$hostname completion status: $status - no mail sent\n"; +} diff --git a/install.sh b/install.sh index 0d1ccad..2a32caf 100755 --- a/install.sh +++ b/install.sh @@ -231,6 +231,7 @@ install_exec crashkernel/startup-crashmemory "$DESTDIR$usr_sbindir/ install_data crashkernel/startup-crashmemory.service "$DESTDIR$systemdunitdir/startup-crashmemory.service" install_exec mxsnoop/mxsnoop.sh "$DESTDIR$usr_sbindir/mxsnoop.sh" install_exec net_qdisc_nfs_low/net_qdisc_nfs_low "$DESTDIR$usr_sbindir/net_qdisc_nfs_low" +install_exec cronwrap/cronwrap "$DESTDIR$usrlocal_bindir/cronwrap" postinstall exit