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.hereNotice 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?
Time flies when you don't know what you're doing
In reply to Sanity check: Tiny wrapper script for /bin/mail by FloydATC
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |