Capture exit code for remote script?

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.

#!/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 grab (if possible) the return code from "/opt/process/verify.sh" ie

  • if fail, return "1" and send an email to the administrator
  • if successful, return "0" and send an email to the developer.

I started reading about the trap command. Can I use it for this purpose? Is there any other option?

+6
source share
3 answers

ssh returns the return value of the corresponding command, or 255 if an error occurred in ssh . Just check this value and take appropriate action.

+18
source

Can you use $? variable to get the response code. For instance:

 % ssh somebox /bin/true % echo $? 0 % ssh somebox /bin/false % echo $? 1 
+8
source
 ssh user@remotehost "/opt/process/verify.sh" echo $? 

echo $? print return code

+5
source

Source: https://habr.com/ru/post/898784/


All Articles