Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

#!/bin/sh
ssh -fCN -D 10000 server.com
firefox //To lauch firefox using Ubuntu
? Code to determine when firefox has quit
Code that kills the tunnel

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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

#!/bin/sh

# Create the tunnel
ssh -fCN -D 10000 server.com
pid=$!

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

# Launch Firefox
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 server.com (instead of tunnel), right ? –  ssssteffff Dec 11 '12 at 22:30

Your Answer

 
discard

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.