[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [SAGE] Output from "cron" command



On Mon, Apr 28, 2003 at 02:42:36PM -0400, Tom Reingold wrote:
> Very good advice, knowing what NOT to use. Is there something we SHOULD use?
> 
> By the way, /usr/ucb/Mail is a link to /usr/bin/mailx in Solaris, but 
> they behave very slightly differently, by examining argv[0]
> 
> Tom

I've been using this little perl script (called mailif)
to direct cron errors to the appropriate location for
sometime now - I like it a lot. I can
   send errors to the right address
   ignore stuff that I can ignore via reg exp's
   make the From line like I want it
   I can Cc whereever I need to
   etc

If nothing else, fix the #! line and run
   mailif -H
to see the help stuff. It's a handy script.

#!/usr/local/bin/perl5

# Getopt takes a string with ":" indicating a value to be set.
sub Getopt
{
    my($control) = shift;
    my($ok) = 1;
    my $flag;
    while( @ARGV && $ARGV[0] =~ /^-(.*)/ )
    {
	my $parm = $1;
	shift @ARGV;
	last if $parm eq '-';
	while( length $parm )
	{
	    ($flag, $parm) = split(//,$parm,2);
	    if ( $control =~ /$flag:/ )
		{ $opt{$flag} = length($parm) ? $parm : shift @ARGV; $parm='' }
	    elsif ( $control =~ /$flag/ )
		{ $opt{$flag}++ }
	    else
		{ warn "$0: Unknown flag $flag\n"; $ok = 0 }
	}
    }
    $ok;
}

$usage = "usage: $0 [-H] [-v RE] [-t to] [-c cc] subject\n";
$started=0;

# extract recipient addresses from header.
$opt{'m'} = '/usr/lib/sendmail -t -or 10m';
$opt{'f'} = $opt{'t'} = (getpwuid($>))[0] || 'root';

Getopt('Hc:e:f:m:s:t:v:') || die $usage;

$opt{'H'} && die <<"End"
$usage
This reads standard input, and exits silently unless there is some
material on it which doesn't match the regular expression.

-f name		Mail "From" address. Default is the owner of the effective uid.
-t name		Mail "To" address. Default is the owner of the effective uid.
-c name		Mail "Cc" addresses. None by default.
-v re		Regular expression to ignore. You probably want to anchor this.
		Default is not to ignore any lines.
-e re		Regular expression to match. Default is to match all lines.
		If both -e and -v match a line, it is not matched.
-s number	Number of seconds to wait for more matching rows.
-m command	Mail command to use. Default is $opt{'m'}.
		Null value means write to stdout.
-H		You're reading it.

The arguments are concatenated to form the Subject line of the mail.
End
;

$SIG{ALRM}=stop;
$SIG{HUP}=$SIG{INT}=$SIG{TERM}=$SIG{QUIT}=$SIG{PIPE}=superstop;

while(<STDIN>)
{ if(!(defined $opt{'v'} and /$opt{'v'}/o ) and (!defined $opt{'e'} or /$opt{'e'}/o ))
  { if($started==0)
    { &start;
    }

    print $_;
    alarm $opt{'s'} if $opt{'s'};
  } else
  { if($started)
    { print $_;
    }
  }
}

exit 0;

sub superstop
{ close(STDOUT);
  exit 0;
}

sub stop
{ close(STDOUT);
  $started=0;
}

sub start
{ open( STDOUT, "|$opt{'m'}" ) || die "$0: Cannot open $opt{'m'}: $!"
    if $opt{'m'};

  print "Subject: @ARGV\n";
  print "To: $opt{'t'}\n";
  print "From: $opt{'f'}\n";
  print "Cc: $opt{'c'}\n" if $opt{'c'};
  print "\n";
  $started=1;
}

--
John
_________________________________________________________
John Mahoney                                jtm@shore.net