Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've set up port forwarding over ssh so I can securely connect to a web console. Since the bash command is pretty long, I want to create a small script that will do the following;

  • Check if there is an open ssh connection to the server in question.
  • If it's not open, open it.
  • Open browser to the console I want to access.

This is what I have so far


ssh -N -l my_username -i ~/.ssh/id_rsa_ssh -L <port>:<ip>:<port> <forwarding_ip> > /dev/null 2>&1 < /dev/null &

open -a Firefox http://localhost:<port>/url/to/something/secure

The ssh connection opens, but the browser can't establish a connection. Is there any way to programmatically open an ssh connection, background it, and ensure it stays open?

share|improve this question
up vote 2 down vote accepted

You wanted autossh.

autossh - Automatically restart SSH sessions and tunnels

share|improve this answer

Your Answer


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.