4

When attempting to establish an ssh tunnel, I noticed that even if the connection fails, the process stays alive. For example, if I try to run this command while hostname is down:

/usr/bin/ssh -f -i /home/user/.ssh/id_rsa -N -R 3000:localhost:22 user@hostname

Occasionally I get the response:

Warning: remote port forwarding failed for listen port 3000

I only get this error message when the original process (running on the local machine) dies but the remote server does not realize yet. The process tries to restart but the server thinks it still has a connection on 3000 and won't except a new connection, resulting in the warning above.

But if I do a pgrep -x ssh I can see that the process is still alive. I would like to run this ssh command as part of a bash script in a cronjob which first checks to see if the tunnel is established and if not reestablishes it, but the way I have the script setup it either a) sees that the tunnel is down and attempts to create a new one (which secretly fails), or b) sees that the failed process is alive and does nothing. The result is that the tunnel never gets reestablished so long as that failed process still exists.

Is there a way to just kill the process if the connection fails instead of getting a warning?

2
  • Is the connection definitely dead? I see this quite a bit when I leave port tunneling sessions open for a very long time, but they often are still working. In my case it seems to be a temporary failure, maybe due to a timeout or something similar with the local process. The tunnel is still working after several messages build up over a period of time, in my case.
    – 0xSheepdog
    Apr 18, 2017 at 17:17
  • 1
    The connection is definitely dead, check out my edits. Apr 18, 2017 at 17:32

2 Answers 2

5

For anyone else who may find the answer, I found the option that I wanted in this answer: adding -o ExitOnForwardFailure=True to the command forces ssh to exit if the port forwarding failed, instead of creating a zombie process:

/usr/bin/ssh -f -i /home/user/.ssh/id_rsa -N -R 3000:localhost:22 user@hostname -o ExitOnForwardFailure=True
0

give ClientAliveInterval a shot.

man sshd_config

     ClientAliveInterval
         Sets a timeout interval in seconds after which if no data has been received from the client, sshd(8) will send a message through
         the encrypted channel to request a response from the client.  The default is 0, indicating that these messages will not be sent
         to the client.
1
  • This is a sshd option, not a ssh option. I need an option that I can add to my original command, if possible. Apr 20, 2017 at 17:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .