Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

When I close my MacBook with an active SSH session in Terminal and then wake it up, the prompt goes unresponsive.

In about five minutes it says

Write failed: Broken pipe

and terminates.

This also happens when I don't type anything in the Terminal in about fifteen minutes.

Can I force OS X to:

  1. Keep SSH connection alive for longest time possible
  2. Kill it immediately once it becomes unresponsive

?

share|improve this question

2 Answers 2

up vote 25 down vote accepted

For keeping the connection alive, you can check in /etc/ssh_config the line where it says ServerAliveInterval, that tells you how often (in seconds) your computer is gonna send a null packet to keep the connection alive. If you have a 0 in there that indicates that your computer is not trying to keep the connection alive (it is disabled), otherwise it tells you how often (in seconds) it is sending the aforementioned packet. Try putting 120 or 240, if it is still killing your connection, you can go lower, maybe to 5, if with that number it doesn't happen, maybe it is your router who is dumping the connection to free memory.

For killing it when it gets hang up, you can use the ssh escape character:

~.

That is, press the tilde and then the period, if it doesn't work, press Enter before you press that, that will kill the connection immediately.

share|improve this answer
    
Thanks! This seems to have worked. –  Dan Jan 14 '12 at 0:06
    
Ok, you can kill the connection with ~., great tip! But what does it actually do? –  CousinCocaine Apr 17 at 12:21
    
The only thing I was confused about was wether or not I had to restart sshd, but it seems as though just exiting the session and reconnecting did the trick –  dgig Jun 20 at 18:02

You can also try using the awesome mosh project. It will gracefully handle network losses, machines going to sleep, etc and happily restore the connection when you have a working network again.

Another more time-honored solution is to use tmux on the remote machine. In that case you may still get a broken pipe, but if you reconnect your shell and applications will be waiting just as you left them.

share|improve this answer
    
Actually, that's exactly what I'm doing now. –  Dan Nov 8 '13 at 19:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.