linux – 如何将cron输出发送到null并将错误发送到电子邮件?

我有一个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的实际调用可能有点时髦,我现在没办法测试它.

我希望这有帮助.

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...