How to stop ssh (via cygwin) from hanging on server disconnect/timeout

2

3

Scenario:

ssh user@host
# good connection, do some work...
# stop working, the server has some function where it automatically disconnects ...
# I come back to my cygwin window and it is unresponsive to ctrl + c/z and finally after maybe 2-5 minutes it comes back with the following error message:
Write failed: Connection reset by peer
MyLocalUsername@MyLocalComputer ~
$
# Now I can resume my cygwin session and reconnect.

My problem: is that I do not like how cygwin hangs. I don't want to force close it and I am not too found of having to open another window (the point of using cygwin for me is to stay on the keyboard and not loose productivity by going back to windows. In short, I would rather ctrl + c to work or for it to not hang at all.

Zombies

Posted 2014-01-18T16:29:50.990

Reputation: 3 214

Answers

2

This works for me on Cygwin64 on Windows 8:

1) Initiate the SSH connection with -e "~" to set the escape character as tilde. It should be the default escape char, but it didn't work for me until I used the -e option. Apparently you can also set the EscapeChar ~ directive in your SSH Config.

2) When your SSH session becomes unresponsive, press Enter, ~, .. You may or may not need to press the tilde twice to actually send it. I read in a lot of places that it's necessary, but it works for me pressing only once.

This answer is based on this one and its comments: https://superuser.com/a/98565/10167

Rafael Almeida

Posted 2014-01-18T16:29:50.990

Reputation: 589

0

Here's a solution for a limited set of cases: If the function that disconnects is something predictable that you explicitly call, and you just want to exit cleanly after invoking it, and if you know this command will take longer than a second to cause the disconnect, you can use nohup, sleep and a parallel call to exit. For example, if your command is guaranteed to take more than one second to cause the disconnect, you can invoke it like this:

nohup <slowcommand> & (sleep 1 && exit)

This guarantees:

  • slowcommand will begin running before exit is invoked
  • the shell will exit before slowcommand causes a disconnect (if slowcommand takes more than a second to cause the disconnect)
  • slowcommand will not be terminated by the call to exit

Keith Russell

Posted 2014-01-18T16:29:50.990

Reputation: 1

0

You can add an option to your ssh command, try this:

ssh -o ServerAliveInterval=30 username@host

This should prevent from disconnecting you from host.

popielasty

Posted 2014-01-18T16:29:50.990

Reputation: 1