簡體   English   中英

使用bash腳本和Capistrano獲取退出代碼

[英]Get exit code with bash script and Capistrano

我有一個執行bash腳本的Capistrano任務:

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 ,每個錯誤返回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

您可以通過在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM