SSH Process not Killed on Remote Machine when the Remote System Unexpectedly Disconnects

Try screen or tmux. On the server side, or both sides if you like:

sudo apt-get install tmux

After logging in, start tmux.

tmux

If the connection breaks, log in again via ssh and reconnect the tmux session. To discover the session number:

tmux ls

The output might be something like this:

0: 1 windows (created Tue Dec 25 19:20:40 2012)

Connect to the tmux numbered session like this:

tmux attach -t 0

Then you are right back where you left it. You won't have to kill the sessions. They just wait for your next visit. Or, if you just want to kill the session...

On the server side, see the /etc/ssh/sshd_config setting for ClientAliveInterval and ClientAliveCountMax. (man 5 ssh_config)

ClientAliveInterval 300
ClientAliveCountMax 0

Once they have been changed to your preference, restart the SSH server.

sudo /etc/init.d/ssh restart

The 300 is seconds (five minutes).


You could try mosh Install mosh, a replacement for SSH built on top of SSH. It supports and roaming and intermittent connectivity, so your Mosh (SSH) session would just continue where you left off whenever your Internet connection gets re-established after an interruption.

You will need to install Mosh on both machines.

References:

  • Mosh's website