How I use ssh's connection sharing feature
Following on the basic overview, here's how I use OpenSSH's connection sharing feature.
First, since I hate programs that don't exit when I want them to, I
ssh automatically create connection masters. Instead I
always explicitly create them myself, so that I can do so in a way that
harmlessly parks them in the background. So while I set a
(to a value mentioned in the previous entry),
ControlMaster set to
no, the default.
When I want to create a master
ssh, I have a script that boils down
ssh -M -f -o "BatchMode yes" $HOST exec sleep 120
You might wonder about the '
One of the peculiarities of how I use connection sharing is that so far
I always do it to hosts that I also have persistent sessions to. These
persistent sessions pin the master
ssh in place whether or not the
ssh itself is still doing anything, so the
sleep does two
things here. First, it keeps the master
ssh going long enough for my
real sessions to start, and second, it means that if and when I later
tear down all of my real sessions to the host, the master
also exit because the
sleep has long since finished.
(Ideally there would be a
ssh option for 'keep running N seconds after
the last session closes to see if a new subordinate shows up'. Then I
could set that to 120 seconds or so and just use '
/bin/true' as the
command that the master
In actual practice I don't create master
ssh instances by hand.
Instead I do it from my X startup file, which (vastly simplified)
winds up looking like:
ssh-master host1 sleep 1 # give it time to come up ssh host1 x-app-1 & ssh host1 x-app-2 & ....
(And then anything else that I want to do on host1 later will also get the benefits of using the existing connection.)
Among other things, this approach has the benefit that it's easy to add. I didn't need to change anything that my X startup script was already doing; I just inserted an additional command early on. (And then took it out again the first time I tried it, when it turned out to cause various problems.)