I open an SSH tunnel programatically, with:

sshpass -p "my_password" ssh -fN -o StrictHostKeyChecking=no -o ExitOnForwardFailure=yes remote_user@remote_server.tld -L 13306: sleep 3

and works as expected, but don't close after 3 seconds of inactivity.

I take the SSH PID and kill explicitly when the connection is finished; but I prefer the sleep version, if can close the tunnel automatically.

The use with sshpass maintain the tunnel open or am I doing something wrong?

  • What about using: timeout 3 sshpass ... ? Or you want exactly use an sshpass option that shuts down the tunnel after X seconds of inactivity? I mean: you need to clese exactly after 3 seconds or after 3 seconds of inactivity ? – Echoes_86 Sep 27 '16 at 9:14
  • I need some seconds of inactivity (not exactly 3). sshpass finish as soon as is executed, the ssh part remains for ever, and I can't do sshpass [...] timeout 3 ssh [...]. Correction: I can do sshpass [...] timeout 3 ssh [...] but has no effect. – José M. Carnero Sep 27 '16 at 9:19

You are using


Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background. This implies -n. The recommended way to start X11 programs at a remote site is with something like ssh -f host xterm.



Do not execute a remote command. This is useful for just forwarding ports.

Which goes into the background before executing a command (which is suppressed). Therefore it stays in the background. The sleep is never run and it waits to be killed.

| improve this answer | |
  • Removed -N and works perfectly. Thanks. – José M. Carnero Sep 27 '16 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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