[ALUG] Email from cron scripts

Mark Rogers mark at quarella.co.uk
Tue Oct 30 18:03:04 GMT 2007

I have the following in my crontab:
    */5 * * * * /path/to/script.sh | mail -es "mail subject" me at example.com

What "script.sh" does is check whether the mail server is running 
correctly. If it is, then it sends a message to stdout and exits. If it 
is not, then it sends a different message to stdout, then restarts the 
mail server, then exits.

If the mail server is OK, the output to stdout is captured and emailed 
via cron correctly.

If the mail server is not OK, it is successfully restarted but the mail 
does not get sent. In fact, the script never exits:
    $ps ax
        28907 ?        S      0:00 /USR/SBIN/CRON
        28908 ?        Ss     0:00 /bin/sh -c /path/to/script.sh | mail 
-es "mail subject" me at example.com
        28912 ?        S      0:00 mail -es mail subject me at example.com
        28941 ?        S      0:00 /usr/sbin/sendmail -i -FCronDaemon 
-oem root

Clearly "mail" or "sendmail" (in my case the Postfix version) doesn't 
like the mail server being restarted under its feet like this.

How can I get around this? I'd like to capture all the output from 
script.sh (including the output from the mail server restart) if I can.

If it's relevant, in this case restarting the mail server means stopping 
(in this order) postfix, clamav and dspam, then starting them again in 
reverse order. The problem I'm having is dspam dies sometimes (once 
every couple of days) and the script is intended to work around this 
until I fix it.

Mark Rogers // More Solutions Ltd (Peterborough Office) // 0845 45 89 555
Registered in England (0456 0902) at 13 Clarke Rd, Milton Keynes, MK1 1LG

More information about the main mailing list