Bug 1473 - Add option to save PID of a backgrounded ssh process
Summary: Add option to save PID of a backgrounded ssh process
Status: CLOSED WORKSFORME
Alias: None
Product: Portable OpenSSH
Classification: Unclassified
Component: ssh (show other bugs)
Version: 5.0p1
Hardware: All All
: P2 enhancement
Assignee: Assigned to nobody
URL:
Keywords:
: 1594 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-05-29 00:58 AEST by Tim Steiner
Modified: 2021-04-23 14:54 AEST (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tim Steiner 2008-05-29 00:58:10 AEST
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
Comment 1 Damien Miller 2008-05-29 05:12:16 AEST
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.
Comment 2 Darren Tucker 2008-05-29 10:10:58 AEST
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
Comment 3 Tim Steiner 2008-05-30 01:09:20 AEST
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).
Comment 4 Tim Steiner 2008-05-30 01:10:49 AEST
Oops, that should have been ssh.pid above, not pid.sh!
Comment 5 Tom Anderson 2009-12-23 06:48:06 AEDT
@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!).
Comment 6 Damien Miller 2009-12-26 23:41:48 AEDT
(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.
Comment 7 Josh Triplett 2011-03-03 06:46:50 AEDT
*** Bug 1594 has been marked as a duplicate of this bug. ***
Comment 8 Damien Miller 2019-05-31 14:29:10 AEST
I think this can be closed - the equivalent capability exists via the control socket.
Comment 9 Damien Miller 2021-04-23 14:54:58 AEST
closing resolved bugs as of 8.6p1 release