Execution of a command locally after ssh tunneling

I have a script file ./run_app.sh which contains of the following:

ssh -L 8888:localhost:8888 ubuntu@address
./run_website.sh   # it should be executed locally

but it tries to run the second line on the remote machine. Actually, I want to run it on my pc. Any idea?


Hello and welcome to Super User.

After ssh -L 8888:localhost:8888 ubuntu@address, your script founds itself on the remote server.

You can pass the flags -fN to ssh (-f is for fork into background and -N is run no command).

Then, after the tunnel is established, your script resumes execution on the next line on your machine.

The downside is that the ssh connection will remain in the background even after your script is finished.

A workaround is ditching the -N flag and execute sleep 10 on the remote server:

ssh -f -L 8888:localhost:8888 ubuntu@address sleep 10

This way, if the tunnel is not used, either when run_website.sh terminates or after 10 seconds if the script won't use the tunnel (i.e. fails for some reason), it will disconnect automaticaly.

This obviously assumes that run_website.sh uses the tunnel.

If you don't care about automatically terminating the tunnel, then go ahead and simply use -fN:

ssh -fN -L 8888:localhost:8888 ubuntu@address

You can kill the ssh process later if you need.