We use this script to get informed if trouble comes our way and a cronjob doesn't perform as expected. I wrote it 'cause cron sends mail only to the user for whom it exeutes, and one would have to use mail-filter or aliases of one kind or the other, to reach a bunch of people.
This little script simplifies this, while trying not to allow you to shoot yourself in the foot :-)

Edit: corrected the timestamp (should've let my editor reload before cutting and pasting :-)

Edit 2: corrected C<>-pod-element

Edit 3: replaced map with join

Edit 4: replaced @#mail_to +1 with scalar(@mail_to)

Edit 5: fixed scalar($mail_to): scalar(@mail_to)

#!/usr/bin/perl -wT # creator : Tomte # created : Don 07 Nov 2002 10:15:00 CET # version : $Id: mailshell.pl,v 1.7 2003/06/02 08:32:36 tom Exp $ use strict; # # config # # standard recipients my @mail_to = qw(regner@dievision.de); # send if execution succesful? my $send_on_success = 0; # pipe through my $mail_cmd = "/usr/sbin/sendmail -t"; # execute my $command = '/bin/true'; # Mail From: <???> my $from = "mailshell.pl"; # # END config # # running -T $ENV{'PATH'} = ''; $ENV{'BASH_ENV'} = ''; $ENV{'ENV'} = ''; # # parameter "parsen" # untaint if ok # foreach my $i (0..$#ARGV) { # no source-routing, no fancy stuff push( @mail_to, $1) if ($ARGV[$i] =~ m/^-to$/ && $ARGV[$i+1] =~ m/ +^([a-z0-9.]+\@[a-z0-9.]+$)/i); # this should be secure enough, if you use this script, you should + trust yourself :-) $command = $1 if ($ARGV[$i] =~ m/^-cmd$/ && $ARGV[$i+1] =~ m/^([:% +'"\/\.a-z0-9 \-_]+)$/i); # from is localpart only, sendmail shall provide the domain $from = $1 if ($ARGV[$i] =~ m/^-from$/ && $ARGV[$i+1] =~ m/^([a-z0 +-9.]+)$/); # a better solution anybody? toggleOpt(\$send_on_success) if $ARGV[$i] =~ m/^-sos$/; } # stderr auf stdout mappen $command .= ' 2>&1'; # 'autoflush', couldn't possibly hurt!?! $| = 1; my $result = `$command`; $result =~ s/[$;'"]//g if $result; if ( (scalar(@mail_to)) > 0 && ((defined($result) && $result ne '') || + $send_on_success) ){ my $recipients = join ',', @mail_to; open( MAIL, "|$mail_cmd"); select( MAIL); print "To: $recipients\n"; print "From: $from\n"; print "Subject: $command\n\n"; print "executed successful" if (!defined($result) || $result eq '' +); print "Output:\n\n" . $result . "\n" if( $result); select(STDOUT); close(MAIL); } sub toggleOpt { my $to_toggle = shift; $$to_toggle = $$to_toggle == 0 ? 1 : 0; } __END__ =pod =head1 NAME mailshell.pl - send a mail if a programm produced output on stdout/std +err =head1 SYNOPSIS mailshell.pl [-sos] [-to e_mail]* [-from name] [-cmd what to run] =over =item B<-sos> send on success =item B<-to e_mail> C<cc> to address C<e_mail> =item B<-from name> local part of from-address =item B<-cmd cmd> the command to execute =back =head1 DESCRIPTION B<Use this script> if you and others (possibly colleagues) want to be +notified via e-mail, if a command, e.g. run from cron, produced outpu +t on stdout I<or> stderr. Using the C<-sos> switch you will be notified in any case afte +r the command was executed. A C<sendmail> complient mailer is requiered! Basic configuration, the variables on top of the code, should be under +taken. =head1 EXAMPLES =over =item A crontab-entry C<<< */15 * * * * cd /home/tom/tools/bin && ./mailshell.pl -cmd './myc +md.sh' -from 'mycmd' -to name1@domain1 -to name2@domain1 > /dev/null +2>&1 >>> executes ./mycmd and notifies the default recipient as well as name1 a +nd name2 if an output was generated from ./mycmd. =back =cut

In reply to cron-job mail-wrapper by Tomte

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.