SSH Remote command exit code
The reason your ssh
is always returning 0
is because the final echo
command is always succeeding! If you want to get the return code from scan
, either remove the echo
or assign it to a variable and use exit
. On my system:
$ ssh host 'false'
$ echo $?
1
$ ssh host 'false; echo $?'
1
$ echo $?
0
$ ssh host 'false; ret=$?; echo $ret; exit $ret'
1
$ echo $?
1
SSH Remote command exit code always get 0
I should use single quotation marks to double quotation marks.
echo $? will become "0", if I use double quotation marks to execution ssh command.
ssh 127.0.0.1 'ssh_test/test.sh; echo $?'
bash - Capture exit code from remote ssh command
The script appears to be running under set -e
, which means when the ssh
connection fails, the script exits immediately.
SSH capture exit code of remote ssh commands
Okay. It seems that this was a mistake on my end, and everything is working correctly. I wasn't calling one of the commands correctly:
php "$RELEASE_DIR" artisan migrate --no-interaction --force
This fixes it:
php "$RELEASE_DIR/artisan" migrate --no-interaction --force
Get return code from command run on ssh tunnel
This is not related to SSH, but to how bash
handles the exit status in pipelines. From the bash
manual page:
The return status of a pipeline is the exit status of the last command, unless the pipefail option is enabled. If pipefail is enabled, the pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully. If the reserved word ! precedes a pipeline, the exit status of that pipeline is the logical negation of the exit status as described above. The shell waits for all commands in the pipeline to terminate before returning a value.
If you want to check that there was an error in the pipeline due to any of the commands involved, just set the pipefail
option:
set -o pipefail
your_pipeline_here
echo $? # Prints non-zero if something went wrong
It is not possible to actually send the exit status to the next command in the pipeline (in your case, ssh
) without additional steps. If you really want to do that, the command will have to be split like this:
res="$(/home/mycode.sh '20'${ODATE} 1)"
if (( $? == 0 )); then
echo -n "$res" | ssh -L 5432:localhost:5432 myuser@myremotehost cat
else
# You can do anything with the exit status here - even pass it on as an argument to the remote command
echo "mycode.sh failed" >&2
fi
You may want to save the output of mycode.sh
to a temporary file instead of the $res
variable if it's too large.
How to separate remote ssh exit codes by groups?
From man ssh
:
ssh exits with the exit status of the remote command or with 255 if an error occurred.
So there is only one special exit code. You don't have to keep a list of exit codes for things like "SSH port is closed", "the host is down", and so on.
If your command never exits with 255 or you don't care about that specific code you can use
ssh root@server 'some command'
sshstatus=$?
if (( status == 255 )); then
echo "connection error"
elif (( status != 0 )); then
echo "command error"
fi
If you need to catch all exit codes (even 255) from your command, or you really, really, really, don't want to manually specify any special exit codes, then you have to print the command's exit code on the server side and extract it on the client side:
cmdstatusfile=$(mktemp)
ssh root@server 'some command; status=$?; wait; printf %3d $status' |
tee >(tail -c3 "$cmdstatusfile") | head -c-3
sshstatus=$?
cmdstatus=$(< "$cmdstatusfile")
rm "$cmdstatusfile"
if (( sshstatus != 0 )); then
echo "connection error"
elif (( cmdstatus != 0 )); then
echo "command error"
fi
If you normally pipe ssh ...
into something you can still do so. Simply add your pipe after head ...
.
This approach assumes that some command
always exits normally. With set -e
or exit
inside some command
the exit code wouldn't be printed at the end. In those cases you could print the exit code inside a trap.
Related Topics
Ssl/Qsslsocket_Openssl.Cpp:1414: Error: Q_Ssl_Ctrl Was Not Declared in This Scope Error
How to Overwrite Linux System Files into The Yocto Filesystem
Bash Script to Install Postgresql - Not Working
How to Decide How Much Stack I Can Use After a Call to Pthread_Attr_Setstacksize
Why Is Running Node.Js on Port 80 Might Not Be Safe
Can't Run Dmidecode on Docker Container
How to Search The Content of a PDF File in Linux Shell Script
Launching Sonar Scanner from a Gitlab Docker Runner
How to Extract Value from JSON Contained in a Variable Using Jq in Bash
Kernel Preemption While Holding Spinlock
Suppressing Compile Time Linkage of Shared Libraries
Shell Exclamation Mark Command
Perl Signal Processing Only Works Once When Sighandler Calls Subroutine
Removing First 3 Characters of File Names in Linux
How to Change Some "Special" Gui Colors in Eclipse