SSH connection automatically closed when a command returns an exit status different from 0

connectionssh

When I execute a command (e.g. : grep, cat, …) on my VPS server that returns an exit status other than 0, my SSH connection is automatically closed.
I have no idea what could involve this issue…

Have you already experimented such kind of issue? Any suggestion?


Hereby the demonstration of this issue:

1) I connect to my VPS server from my computer:
sylvain@mycompute

sylvain@mycomputer:~$ ssh myuser@vps-server
Last login: Sat Sep  9 17:02:30 2017 from 11.22.33.44
myuser@vps-server:~$ 

2) I create a script that gets the exit status from the 1st parameter:

myuser@vps-server:~$ chmod u+x /tmp/test-exit-status.sh 
myuser@vps-server:~$ cat /tmp/test-exit-status.sh
#!/bin/bash

echo "Exit status will be '$1'."
exit $1

3) When exit status is 0, the SSH connection is kept:

myuser@vps-server:~$ /tmp/test-exit-status.sh 0
Exit status will be '0'.
myuser@vps-server:~$ 

4) When exit status is NOT 0 (e.g. 56), the SSH connection is automatically closed and, on my computer, the exit status of the SSH command is the exit status of the script:

myuser@vps-server:~$ /tmp/test-exit-status.sh 56
Exit status will be '56'.
Connection to vps-server closed.

sylvain@mycomputer:~$ echo $?
56

Best Answer

Sounds as if you have set -e in your ~/.bashrc

Try doing set +e and re-running your test to see if it does the same thing again.

I'd consider adding explicitly set +e (or removing a set -e) in your ~/.bashrc to permanently fix it.