linux – 如何将cron输出发送到null并将错误发送到电子邮件?
发布时间:2020-12-13 23:49:41 所属栏目:Linux 来源:网络整理
导读:我有一个cron作业,我想将其输出发送到/ dev / null但是如果发生错误,那么它应该发送一封电子邮件. 否则我每天收到一封关于cron输出的电子邮件,我很难看到错误发生的时间. 解决方法 怎么样 59 23 * * * { tmpFile=/tmp/yourCmdErrs.$$; export tmpFile ; your
我有一个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的实际调用可能有点时髦,我现在没办法测试它. 我希望这有帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |