bash通过电子邮件发送错误

时间:2012-11-14 09:00:07

标签: bash error-handling bash-trap

我写了一些做一些备份工作的bash脚本。我使用errexit和pipefail运行脚本,所以我不会错过任何错误。我现在想要的是脚本向我发送电子邮件以防发生错误。我得到了这个工作。但我希望脚本错误包含在电子邮件正文中。我怎么能这样做?

这是脚本:

#!/bin/bash
# exit script if an error occures
set -o errexit
# even exit if an error in passed through a pipe
set -o pipefail

trap 'ERRORMESSAGE_HERE | mailx -s "Something went wrong on srv-002" mymail@mycompany.ch' ERR

# the backup job here

# if script reaches this point everything went perfectly good
echo "all good!"

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:3)

如何(未经测试):

{
    # do the work here
} 2> >(mailx -s "job errors" recipient@example.com)
相关问题