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
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.
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.
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
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 $?'
exit code from ssh command
This code
ssh -i key/keyId.ppk user@X.X.X.X "
grep blabla ddd
if [ ! $? -eq 0 ]; then exit 1; fi
"
evaluates $?
in your shell and not in the remote one, because the $
is not escaped in single quotes. You should escape that to reach desired behaviour. Once to avoid evaluation in your local shell, for the second time to avoid evaluation when it is passed to the bash on remote side. Or rather put the command into single quotes:
ssh -i key/keyId.ppk user@X.X.X.X '
grep blabla ddd
if [ ! $? -eq 0 ]; then exit 1; fi
'
Related Topics
Can Perf-Stat Results Be Generated from a Perf.Data File
Gui Svn Client for Debian Linux
Elastic Beanstalk: Log Task Customization on Amazon Linux 2 Platforms
How to Enable The Docker Remote API on Windows
How to Disable Tcp Slow Start in Linux
Replace Forward Slash with Double Backslash Enclosed in Double Quotes
Amazon Linux Ami Ec2 Gui/Remote Desktop
Default Font Size of Javafx Under Linux Is Larger as on Windows
How to Zip Folder That Contains More Than 12Gb Data
Where Is Default Installation Directory for Mongodb
Poor Memcpy Performance in User Space for Mmap'Ed Physical Memory in Linux
Docker with '-User' Can Not Write to Volume with Different Ownership