[英]Get exit code with bash script and Capistrano
I have a Capistrano task excecuting a bash script: 我有一个执行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
The script.sh returns exit 0
for success and exit 1, 2, 3 etc... for each error. script.sh成功返回exit 0
,每个错误返回exit 0
1、2、3等。
When exit is not 0, I'm logging "There was an error running the script". 当exit不为0时,我正在记录“运行脚本时出错”。 But, inside rescue, I want to know the exit status to log messages for specific errors. 但是,在救援过程中, 我想知道退出状态以记录特定错误的消息。
Something like this: 像这样:
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
Or, maybe, show an specific error given by script.sh: 或者,也许显示出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
You can trick it by echoing the exit code in your shell call: 您可以通过在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.