3

我写了一些 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!"

非常感谢你的帮助!

4

1 回答 1

3

怎么样(未经测试):

{
    # do the work here
} 2> >(mailx -s "job errors" recipient@example.com)
于 2012-11-14T11:26:22.263 回答