我有一个cron作业,我想将其输出发送到/ dev / null但是如果发生错误,那么它应该发送一封电子邮件.
否则我每天收到一封关于cron输出的电子邮件,我很难看到错误发生的时间.
解决方法:
怎么样
59 23 * * * { tmpFile=/tmp/yourCmdErrs.$$; export tmpFile ; yourCommand > /dev/null 2>${tmpFile}; if [ -s ${tmpFile} ] ; then mailx -s"errors in yourCommand" < ${tmpFile} ; /bin/rm ${tmpFile} ; fi ; }
爆炸,它是
# set whatevery your time/days are # 59 23 * * *
# my superstition to use open and closing # { }
# set a tmpFile var # tmpFile=/tmp/yourCmdErrs.$$; export tmpFile ;
# run yourCmd save STDERR to file # yourCommand > /dev/null 2>${tmpFile};
# check if tmpFile has anything in it # if [ -s ${tmpFile} ] ; then
# obvIoUs, hopefully # mailx -s"errors in yourCommand" < ${tmpFile}
# cleanup tmpFile # /bin/rm ${tmpFile} ;
# fi
# note that closing ';' is a must when using {} pairs ; }
对mail / mailx的实际调用可能有点时髦,我现在没办法测试它.
我希望这有帮助.