我正在安排以下 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 消息“连接被外部主机关闭”。结果在那里。