![如何抑制特定作业的 cron 的所有电子邮件通知?](https://linux22.com/image/35095/%E5%A6%82%E4%BD%95%E6%8A%91%E5%88%B6%E7%89%B9%E5%AE%9A%E4%BD%9C%E4%B8%9A%E7%9A%84%20cron%20%E7%9A%84%E6%89%80%E6%9C%89%E7%94%B5%E5%AD%90%E9%82%AE%E4%BB%B6%E9%80%9A%E7%9F%A5%EF%BC%9F.png)
我正在安排以下 shell 脚本
#!/bin/bash
(echo open mailserver.nowhere.local 25; sleep 1; echo EHLO; echo quit)\
| telnet | grep "?Invalid command"
if [ $? -eq 1 ]; then
if [ -r /tmp/sendmail_stopped ]; then
rm /tmp/sendmail_stopped
/etc/init.d/sendmail start
mail -s "sendmail has started back up." [email protected] < /dev/null
else
echo "sendmail OK"
fi
else
if [ ! -r /tmp/sendmail_stopped ]; then
touch /tmp/sendmail_stopped
/etc/init.d/sendmail stop
else
echo "sendmail still not OK"
fi
fi
像这样
*/5 * * * * root /home/amr/bin/sendmail_alive.sh 2>&1 > /tmp/sendmail_alive.log
但我仍然收到来自telnet
指挥部的电子邮件。
我尝试了很多方法来抑制获取包含命令输出的电子邮件telnet
,即
Connection closed by foreign host.
我不明白我做错了什么。任何建议,将不胜感激。
答案1
常见错误,重定向顺序错误,试试这个:
… sendmail_alive.sh >/tmp/sendmail_alive.log 2>&1
它的工作原理如下:
- 文件描述符标准输出到
/tmp/sendmail_alive.log
/tmp/sendmail_alive.log
文件描述符 stderr 到 stdout ( )的值
根据您的命令,您首先将 stderr 指向原来的 stdout,然后您会收到 stderr 消息“连接被外部主机关闭”。结果在那里。