bash 通过电子邮件发送错误

标签 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!"

非常感谢您的帮助!

最佳答案

怎么样(未测试):

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

关于bash 通过电子邮件发送错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13375711/

相关文章:

c - 在 C 中通过命令行传递标点符号

linux - 用于验证备份文件夹是否存在的 Bash 脚本

linux - bash 中信号之间的最短时间

linux - 通过脚本中的参数将多个文本文件连接到 Bash 中的单个文件中

linux - 了解 bash 大括号扩展

r - R如何关闭特定错误

reactjs - ReactJS从服务器接收表单错误

php - 在PHP中处理错误或意外结果的最可靠方法是什么?

bash - Trap 命令未检测终端大小调整 (SIGWINCH)

linux - Bash 在 rsync/subshel​​l exec 语句期间不捕获中断