【问题标题】:Get exit code with bash script and Capistrano使用 bash 脚本和 Capistrano 获取退出代码
【发布时间】:2013-12-14 20:01:11
【问题描述】:

我有一个 Capistrano 任务正在执行 bash 脚本:

task :test_task, roles: :ghost do
  begin
    run "./script.sh"
  rescue Capistrano::CommandError => e
    logger.important 'There was an error running the script'
  end
end

script.sh 返回 exit 0 表示成功,退出 1、2、3 等...表示每个错误。

当 exit 不为 0 时,我正在记录“运行脚本时出错”。但是,在救援内部,我想知道退出状态以记录特定错误的消息。

类似这样的:

rescue Capistrano::CommandError => e
  logger.important 'Error message 1' if e.exit_status == 1
  logger.important 'Error message 2' if e.exit_status == 2
  ...
end

或者,也许,显示 script.sh 给出的特定错误:

rescue Capistrano::CommandError => e
  logger.important e.error_message
  #e.error_message this will be 'Error message 1' if exit status equals 1
  #e.error_message this will be 'Error message 2' if exit status equals 2
end

【问题讨论】:

    标签: ruby-on-rails ruby bash capistrano exit-code


    【解决方案1】:

    您可以通过在 shell 调用中回显退出代码来欺骗它:

    run("./script.sh; echo EXIT_CODE=$?") do |ssh_channel, stream_id, output|
      output, exit_code = output.split("EXIT_CODE=")
      logger.important 'Error message 1' if exit_code == 1
      logger.important 'Error message 2' if exit_code == 2
      puts output
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多