How to manage tunnels as background processes from a shell script

command linetunnel

I need to set up a couple of ssh-tunnels from a shell script. I've tried running them as bg tasks using:

ssh -L 3000:server1:5029 me@server2 &
ssh -L 3001:server3:3306 me@server2 &

but the tunnels don't seem to work correctly when I launch them that way.

They work fine when I manually set them up in their own tabs, so my next idea is to have the script open new tabs in Terminal and run the commands in there as foreground processes.

Note: this question was originally "How do I launch a new terminal tab from the shell and then run a command in it?", but I got two answers about dealing with the tunnels. For the "opening a shell" question, I found this on SuperUser, which will work , though new tabs in the background would be preferable to the foreground windows that it opens.

Best Answer

This is technically not an answer to the question asked, but rather an answer to your problem as described. The ssh command has two switches that may be useful to you:

ssh -f -N -L 3000:server1:5029 me@server2

tells ssh to hang around in the foreground just long enough to ask for any necessary passwords, and then put itself in the background, not executing any remote command but just handling the tunnel.

If you really want this to appear in a tab then you may want a different solution.