When sending an ssh session to the background, there is no option to save the forked process's PID to a file. This would be very helpful when writing scripts that manage ssh tunnels. An example of such usage would be: ssh user@host -L 123:remote.host:456 -f /var/run/tunnel.pid -N
You can control a running SSH process using the control socket ssh -nNfL 123:remote_host:456 -MS ~/.ssh/ctl-%r-%h-%p__%l user@host You can ask the master process to exit gracefully using: ssh -S ~/.ssh/ctl-%r-%h-%p__%l -O exit user@host See the ControlPath and ControlMaster documentation in ssh_config(5) for more information.
You can also do it with some trivial shell scripting: #!/bin/sh echo $$ >ssh.pid exec ssh user@host -L 123:remote.host:456 -f -N
Damien: Thanks for the tip. I was unaware of ControlMasters. Darren: Actually, since the ssh process is forking, the pid your script saves is that of the parent process, and not the new child process. I suppose something like this would work: #!/bin/sh echo $$ >pid.sh exec ssh user@host -L 123:remote.host:456 -N The downside is that you have to run it in the background, and ssh won't be able to prompt for a password (if needed).
Oops, that should have been ssh.pid above, not pid.sh!
@Damien: The ControlMaster feature is very cool, but you can't (currently) port-forward with the slaves, so if you're trying to set up multiple port forwardings, it doesn't help. @Darren: Things along this line (including ssh ... & ; echo $!) work in many cases, but crucially, don't work with -f. -f is particularly useful because ssh remains in the foreground until it has completed authentication, so in a script, you can rely on a connection having been made once it returns. What i'd like to be able to do is to write scripts like: echo "Starting port forwarding of A" <SOME SSH COMMAND> echo "Starting port forwarding of B" <SOME SSH COMMAND> echo "Doing lots of things using forwarded port ..." <SOME COMMAND TO CLOSE ALL CONNECTIONS> Whether this is using pid/kill or the ControlMaster mechanism. I think i have an idea - mktemp a directory, then start several independent control master connections, one for each forwarded port, with control paths mktemp'd (or otherwise) in that directory, running with -f. To shut them down, for-loop over the files in the directory and do -O exit to close the connections. It's anything but pretty, but it should work (a classic shell script, then!).
(In reply to comment #5) > @Damien: The ControlMaster feature is very cool, but you can't > (currently) port-forward with the slaves, so if you're trying to > set up multiple port forwardings, it doesn't help. You are correct that it cannot configure port-forwardings (yet - see bug #1617. I hope to fix this before 5.4), but it does help - it gives you exactly what a pidfile does as afar as being able to control a running daemon. > I think i have an idea - mktemp a directory, then start several > independent control master connections, one for each forwarded port, > with control paths mktemp'd (or otherwise) in that directory, running > with -f. To shut them down, for-loop over the files in the directory > and do -O exit to close the connections. It's anything but pretty, but > it should work (a classic shell script, then!). Yes, this is what I mean.
*** Bug 1594 has been marked as a duplicate of this bug. ***
I think this can be closed - the equivalent capability exists via the control socket.
closing resolved bugs as of 8.6p1 release