【问题标题】:capture exit code of child script called via ssh from parent script using shell script使用 shell 脚本从父脚本捕获通过 ssh 调用的子脚本的退出代码
【发布时间】:2015-05-08 19:42:52
【问题描述】:

我有一个名为 deployment.sh 的子脚本和名为 deloy_base.sh 的父脚本。从父 deploy_base.sh,我通过 ssh 在其他服务器上调用子脚本。实际上子脚本正在执行部署过程。如果子脚本中的任何命令失败,则应将错误状态发送到父脚本,以便我可以根据子脚本的成功或失败错误代码发送电子邮件。

现在无论子脚本中的任何命令是否失败,它总是会进入成功块。请帮忙。

 ssh user@10.0.0.1 "/home/scripts/deployment.sh" DEV 2>&1  | tee /home/release/DEV.log
    if [ $? -ne 0 ]
    then
    `mail -s "DEPLOYMENT FAILED" -a /home/DEV.log
     -r "Deployment_Log@example.com" user@example.com > /dev/null 2>&1`
    else
   `mail -s "DEPLOYMENT SUCCESS" -a /home/DEV.log
     -r "Deployment_Log@example.com" user@example.com  > /dev/null 2>&1`
    Fi

【问题讨论】:

    标签: linux shell ssh


    【解决方案1】:

    man bash

    管道的返回状态是最后一条命令的退出状态, 除非启用了 pipefail 选项。如果启用 pipefail,则 管道的返回状态是最后(最右边)命令的值 以非零状态退出,如果所有命令成功退出,则为零。

    在你的情况下$?tee 的退出状态,但是

    set -o pipefail
    

    解决问题。

    【讨论】:

      猜你喜欢
      • 2013-08-28
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-02
      相关资源
      最近更新 更多