2013-11-28 50 views
1

我有一個Capistrano的任務excecuting bash腳本:獲取退出代碼與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成功並退出1,2,3等...的每個錯誤。

當退出不是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 

回答

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