Getting a pid for an ssh process that backgrounded itself

So I wanted to call two background ssh processes:

ssh -D localhost:8087 -fN aws-gateway-vpc1
ssh -D localhost:8088 -fN aws-gateway-vpc2

These gateways don’t have the benefit of letting me set an authorized_keys file, so I must be prompted for my interactive password. That is why I’m using the -f flag and not the shell’s & which will only background the process after I authenticate interactively.

In this scenario I appear to be unable to use the $! bash variable to get the pid of the recently [self] backgrounded process.

What other options do I have to find the correct pid to kill later if interrupted?

Asked By: dlamblin

||

The $! doesn’t work, as you say, because it hasn’t been backgrounded by the current shell. In fact, the ssh process isn’t even a child of the shell you launched it from. On my Arch system, at least, it is run as a child of PID 1, the init process.

So, to get the PID, you can simply use ps:

$ ssh -f  localhost sleep 100
$ ps aux | grep '[s]sh.*-f'
terdon   20648  0.0  0.0  43308   680 ?        Ss   12:15   0:00 ssh -f localhost sleep 100

That tells me the PID is 20648*.

Alternatively, and more simply, use pgrep -f:

$ pgrep -f 'ssh.*-f'
20648

And, to kill it (them):

pkill -f 'ssh.*-f'

* See this question if you’re wondering about the [s] in the grep command.

Answered By: terdon

Finding the pid by grepping might be error prone. Alternative option would be to use ControlPath and ControlMaster options of SSH. This way you will be able to have your ssh command listen on a control socket and wait for commands from subsequent ssh calls.

Try this

ssh -D localhost:8087 -S /tmp/.ssh-aws-gateway-vpc1 -M -fN aws-gateway-vpc1
# (...)
# later, when you want to terminate ssh connection
ssh -S /tmp/.ssh-aws-gateway-vpc1 -O exit aws-gateway-vpc1

The exit command lets you kill the process without knowing the PID. If you do need the PID for anything, you can use the check command to show it:

$ ssh -S /tmp/.ssh-aws-gateway-vpc1 -O check aws-gateway-vpc1
Master running (pid=1234)
Answered By: RafaƂ Krypa

I dont have 50 rep points so I have to answer…
This example is not complete because you have to add multiplex control to ssh.
On Debian its not enabled by default so you have to enable it via ssh_config file or even easier with "-o ControlMaster=yes" option.
Try this example and it works with every ssh. It will tunnel
port 3306 from example.org to your localhost on port 3307.
In the example I added a port option because you also need to give it to the -O check command.

ssh -p1234 -TqfN -L 3307:localhost:3306 user@example.org -o ControlMaster=yes -o ControlPath=/dev/shm/control:%h:%p:%r -S /dev/shm/control:%h:%p:%r

Then you can use check,exit,etc. like this

ssh -p1234 -o ControlPath=/dev/shm/control:%h:%p:%r -O check root@example.org

Master running (pid=xxxx)

Answered By: Jackfritt
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.