I have the following in my crontab: */5 * * * * /path/to/script.sh | mail -es "mail subject" me@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 [..snip..] 28907 ? S 0:00 /USR/SBIN/CRON 28908 ? Ss 0:00 /bin/sh -c /path/to/script.sh | mail -es "mail subject" me@example.com 28912 ? S 0:00 mail -es mail subject me@example.com 28941 ? S 0:00 /usr/sbin/sendmail -i -FCronDaemon -oem root [..snip..]
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
Try using a few script files for this!!! cron a /path/to/launch.sh which is like
#/bin/sh /path/to/script.sh > /tmp/output.txt pause 10 mail -es "mail subject" him@there.com </tmp/output.txt # clear up ( only after the testing is OK !!! ) rm /tmp/output.txt
but it looks like there might be an error because you kill the mail server and the set it on its way. Then immediately send an email, so the server might not be started up in time.
HTH Keith
________________________________
From: main-bounces@lists.alug.org.uk on behalf of Mark Rogers Sent: Tue 30/10/2007 6:03 PM To: ALUG - mailing list Subject: [ALUG] Email from cron scripts
I have the following in my crontab: */5 * * * * /path/to/script.sh | mail -es "mail subject" me@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 [..snip..] 28907 ? S 0:00 /USR/SBIN/CRON 28908 ? Ss 0:00 /bin/sh -c /path/to/script.sh | mail -es "mail subject" me@example.com 28912 ? S 0:00 mail -es mail subject me@example.com 28941 ? S 0:00 /usr/sbin/sendmail -i -FCronDaemon -oem root [..snip..]
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
_______________________________________________ main@lists.alug.org.uk http://www.alug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
keith.jamieson@bt.com wrote:
Try using a few script files for this!!! cron a /path/to/launch.sh which is like
That sounds like a reasonable idea, I'll give it a try.
but it looks like there might be an error because you kill the mail server and the set it on its way. Then immediately send an email, so the server might not be started up in time.
I think (I'm probably wrong) that when I piped directly into mail, mail must have opened a connection to the mail server at that point, so killing mail after that broke things. I'm pretty sure that mail is up by the time the script finishes (indeed I added a "postqueue -f" to start processing any backlog, which would have complained if postfix was not up by that point but did not).
On 30 Oct 2007, at 6:03 pm, Mark Rogers wrote:
I have the following in my crontab: */5 * * * * /path/to/script.sh | mail -es "mail subject" me@example.com
Are you aware that cron will email the output of the script it processes the user whose crontab it is running (or elsewhere if you specify by setting MAILTO) automatically unless you redirect it elsewhere?
Thanks,
Dave
David Reynolds wrote:
Are you aware that cron will email the output of the script it processes the user whose crontab it is running (or elsewhere if you specify by setting MAILTO) automatically unless you redirect it elsewhere?
Yes, but I wanted to set the subject and control the destination of the email on a per-task basis.
Incidentally I did initially just set MAILTO but that didn't seem to work (can MAILTO be an email address or does it need to be a user?)
On Wed, Oct 31, 2007 at 09:06:50AM +0000, Mark Rogers wrote:
David Reynolds wrote:
Are you aware that cron will email the output of the script it processes the user whose crontab it is running (or elsewhere if you specify by setting MAILTO) automatically unless you redirect it elsewhere?
Yes, but I wanted to set the subject and control the destination of the email on a per-task basis.
Incidentally I did initially just set MAILTO but that didn't seem to work (can MAILTO be an email address or does it need to be a user?)
Yes it can be an e-mail address, I use it a reasonable amount on various machines...