bash 捕获远程脚本的退出代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7677948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capture exit code for remote script?
提问by Chris
I have a shell script that runs on my server every day. It does some house cleaning and connects to a remote host to perform other tasks i.e.
我有一个每天在我的服务器上运行的 shell 脚本。它进行一些房屋清洁并连接到远程主机以执行其他任务,即
#!/bin/bash
#do something...
...locally...
#run remote script...
ssh user@remotehost "/opt/process/verify.sh"
exit
It works fine but to be safe I would like to capture (if possible) the return code from "/opt/process/verify.sh" i.e.
它工作正常,但为了安全起见,我想捕获(如果可能)来自“/opt/process/verify.sh”的返回码,即
- if fail, return "1" and send email to admin
- if success, return "0" and send email to developer.
- 如果失败,返回“1”并向管理员发送电子邮件
- 如果成功,返回“0”并发送电子邮件给开发人员。
I started reading about the command trap. Can I use it for that purpose? Is there another option?
我开始阅读有关命令的信息trap。我可以为此目的使用它吗?还有其他选择吗?
回答by Ignacio Vazquez-Abrams
sshreturns the return value of the command in question, or 255 if an error occurred in sshitself. Simply check this value and take appropriate action.
ssh返回有问题的命令的返回值,如果ssh本身发生错误,则返回 255 。只需检查此值并采取适当的措施。
回答by Alfred Fazio
You can use the $? variable to get the response code. For instance:
您可以使用 $? 变量以获取响应代码。例如:
% ssh somebox /bin/true
% echo $?
0
% ssh somebox /bin/false
% echo $?
1
回答by Alessandro Pezzato
ssh user@remotehost "/opt/process/verify.sh"
echo $?
echo $?print return code
回声 $? 打印返回码

