linux - 在 shell 脚本中,如何在执行到标准输出(不是标准错误)时回显 shell 命令?

标签 linux shell cron

我想编写一个简单的 cron 作业包装器,以下列方式扩展 native cron:

  1. 仅当作业打印出标准错误或作业的返回码不为 0 时才发送电子邮件。
  2. 它将所有执行的命令(及其输出)记录到日志文件中。

这是我的问题,我希望在 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/

相关文章:

shell - 生成shell脚本调用树

mysql - 使用 phpMyAdmin 从 mySQL 自动导出 CSV

amazon-web-services - AWS CloudWatch 中的 cron 表达式 : How to run once a week

linux - 需要适用于 Linux 的文本转语音和语音识别工具

C 脚本在全局范围内不起作用

bash - 如何使用 shell 脚本和 'xrandr' linux 命令打开/关闭屏幕监视器?

Linux 如何确定 cron 配置文件的位置?

c - 从函数 : Error 返回字符串指针

php - ZipArchive 类 PHP 安全问题

linux - 对每个主机使用自定义参数的并行 SSH