我想编写一个简单的 cron 作业包装器,以下列方式扩展 native cron:
- 仅当作业打印出标准错误或作业的返回码不为 0 时才发送电子邮件。
- 它将所有执行的命令(及其输出)记录到日志文件中。
这是我的问题,我希望在 cron 作业中执行的所有命令都被记录(上面的项目符号 #2),但是 /bin/bash -x job.sh
(或 set -x
inside job.sh
) 将触发警报电子邮件(突破上面的项目符号 #1)。
最佳答案
来自 bash(1)
手册页:
BASH_XTRACEFD
If set to an integer corresponding to a valid file descriptor, bash will write the trace output generated when
set -x
is enabled to that file descriptor. The file descriptor is closed when BASH_XTRACEFD is unset or assigned a new value. Unsetting BASH_XTRACEFD or assigning it the empty string causes the trace output to be sent to the standard error. Note that setting BASH_XTRACEFD to 2 (the standard error file descriptor) and then unsetting it will result in the standard error being closed.
...
BASH_XTRACEFD=1
set -x
关于linux - 在 shell 脚本中,如何在执行到标准输出(不是标准错误)时回显 shell 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45873631/