5

I'm experiencing a bizarre problem with SSH + Bash + Terminal.app: when SSH is run in the background, the shell appears to randomly lose keypresses ~50% of the time.

For example, if I run:

$ ssh -N my-tunnel &
$ # typing becomes lossy

Then type "asdf", only the letters "a" and "f" might show up (although this is random and partially related typing speed; if I was typing at full speed, possibly only the "f" might appear).

This problem only affects the shell. If I run, for example, ssh -N my-tunnel & cat, characters are echoed as expected from cat. Key presses are lost once cat is killed and I'm back at the shell.

Other things to note:

  • Other terminal windows are unaffected
  • The shell returns to normal as soon as ssh is terminated
  • Using nohup ssh -N my-tunnel & doesn't change anything

So: what's going on here? And is there any way I can background ssh without breaking the terminal?

Kyle Jones
  • 14,845
  • 3
  • 40
  • 51
David Wolever
  • 4,654
  • 4
  • 17
  • 15

2 Answers2

4

You should be using -n in addition to -N to prevent ssh from reading from the terminal.

Kyle Jones
  • 14,845
  • 3
  • 40
  • 51
  • Bad idea: if `ssh` needs a password, it won't work. See my answer. – vinc17 Sep 08 '14 at 21:00
  • 3
    Well, if he's running it with &, ssh obviously does not need a password. – Kyle Jones Sep 08 '14 at 21:02
  • That's precisely why `&` is a bad idea. The `-f` option has been designed to take care of passwords. Using this option is the right solution, which works in all cases. – vinc17 Sep 08 '14 at 21:04
  • Unfortunately `-f` doesn't play well with `ControlPersist`. When using `ssh -fN` the connection is closed after `ControlPersist` seconds. – David Wolever Sep 09 '14 at 01:59
3

This is the normal behavior. Do not run ssh in background when it is still connected to the terminal. If you want to put it in background once the terminal is no longer needed (e.g. for a password), use the -f option and do not use &:

ssh -fN my-tunnel
vinc17
  • 11,912
  • 38
  • 45
  • The difficulty with using `-f` is that the connection is closed after a timeout of `ConnectionPersist` :/ – David Wolever Sep 08 '14 at 22:14
  • @DavidWolever I suppose that you mean `ControlPersist`, but where do you see that this is specific to `-f`? – vinc17 Sep 08 '14 at 22:24
  • Err, yes, I mean `ControlPersist`. It's relevant to `-f` because, `ControlPersist` seconds after running `ssh -fN`, the connection is closed. If I remove `ControlPersist` the connection isn't closed. – David Wolever Sep 09 '14 at 01:58