我写了一些 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!"
非常感谢您的帮助!
最佳答案
怎么样(未测试):
{
# do the work here
} 2> >(mailx -s "job errors" recipient@example.com)
关于bash 通过电子邮件发送错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13375711/