I frequently start background SSH tunnels using the ... -f -N -L ... options. This works great, until for example I sleep my machine, or my VPN credentials expire, when the tunnels stop working and I have to dig through the output of ps to kill the old ones and restart. The symptom of the broken tunnels is a "Broken pipe".

Is there any way to instruct the ssh process to exit when its pipe breaks? Then I could just restart my SSH tunnels when they die, and skip ps.

  • autossh manages that, I think – Rui F Ribeiro Aug 16 '18 at 15:15

I'd try to set ServerAliveInterval to something higher then default 0(never)

And instead of running those tunnels into background (with -f) I'd put it into systemd units with Restart=always option

  • ServerAliveInterval doesn't help with for example a laptop, which sleeps/loses connectivity frequently. And the systemd unit would only know to restart the process if it dies, which SSH tunnels don't do for broken pipes. If I could instruct the tunnel to die on a broken tunnel that would help a a lot. – qwzy Aug 19 '18 at 17:28

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.