Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 am using Ubuntu 12.04 64-bit, with all updates installed.

I made a simple shell script that starts a SOCKS5 tunnel and launches Firefox (with correct network proxy settings) to use the tunnel. How do I recognize when Firefox has ended (when I close it) so that I can close the tunnel? Also, it would be awesome if I could run this in the background, but not necessary.

ssh -fCN -D 10000
firefox //To lauch firefox using Ubuntu
? Code to determine when firefox has quit
Code that kills the tunnel

share|improve this question
up vote 0 down vote accepted

You could use the trap command to tell the terminal to kill the tunnel when it exits :


# Create the tunnel
ssh -fCN -D 10000

# Tell the terminal to kill the tunnel on exit or error
trap "kill $pid" EXIT

# Launch Firefox

When you exit Firefox, the shell will exit and kill the tunnel.

share|improve this answer
Thanks! I had to change the pid=$! because using ssh with -f causes a fork, which the shell cannot see. Therefore, I changed the variable to capture it correctly. pid=$(echo `ps ax | awk '/100000\ tunnel/{print $1}'`) – Al_Jehle Dec 10 '12 at 0:54
You mean (instead of tunnel), right ? – ssssteffff Dec 11 '12 at 22:30

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.