bash通过电子邮件发送错误

bash通过电子邮件发送错误,bash,error-handling,bash-trap,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

我写了一些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!"
非常感谢你的帮助

怎么样(未经测试):


-这似乎是一个相关的问题,看看答案是否适合你。这应该是可行的。或者只需附加
2>>(mailx-s“作业错误”recipient@example.com)
当您运行脚本时(如果您使用
cron
进行调度),或者编写一个简单的包装器来完成此操作(如果您手动运行)。我确认,它工作得非常好!我的测试:
ls notExistingDir 2>>(mailx-s“作业错误”recipient@example.com)
{
    # do the work here
} 2> >(mailx -s "job errors" recipient@example.com)