Problem: Many many crontab entries that used to send mail to root in case of problems automatically by crond must now be tagged with subject lines for classification. Output is generated only if the scripts detect problems so in most cases the pipeline will be empty, causing '/bin/mail' to complain. Obviously I don't want those empty messages. Some versions of '/bin/mail' support a setting 'nonullbody', mine unfortunately doesn't seem to.

Solution: Write a small wrapper to '/bin/mail' called '/usr/bin/mail-if', which only calls '/bin/mail' if the pipeline is non-empty:

#!/usr/bin/perl use strict; use warnings; my @lines = <STDIN>; if (@lines) { my $cmd = join(' ', '/bin/mail', @ARGV); open(my $ch, '|-', $cmd) || die "Error executing '$cmd': $!"; foreach my $line (@lines) { print $ch $line; } close $ch; }

Example use:

05 12 * * * root /home/atlas/tools/CRON_macscanner | mail-if -s "[Nettverk][Error] CRON_macscanner" email@address.here

Notice that if the cron job itself generates errors on STDERR those messages will still be sent to root, this is by design.

Question: This appears to work as intended. Does anyone see obvious problems with this approach, or perhaps a simpler solution?

-- FloydATC

Time flies when you don't know what you're doing


In reply to Sanity check: Tiny wrapper script for /bin/mail by FloydATC

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.