2011-05-12 39 views

回答

2

如何

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 ; } 

实际来电来邮/ mailx的可能会稍微时髦,我也没有办法,现在来测试它。

我希望这会有所帮助。

3

会建议你只需修改你从cron打电话来管标准输出脚本的输出到/ dev/null的:所有输出到/ dev/null,而标准错误转到MAILTO目标。

2
my_program 2>&1 1>/dev/null | mailx -s "Subject" recipient ... 

虽然违反直觉(对我来说),重定向似乎是由shell从右向左处理的。该序列首先丢弃stdout ,然后将stderr重定向到stdout。

相关问题