Reputation: 173
I need to find pid of proccess started in bash script with another program. I use sshpass to enter the password, sshpass start ssh client and that client open sshtunnel to server. Here is examples:
start_tunnel.sh
#!/bin/bash
exec sshpass -p 'passw' ssh -D :port user@$server -o StrictHostKeyChecking=no -f -N
exit
And I start it with subrocess.Popen
:
proc = subprocess.Popen('start_tunnel.sh')
The script start just fine, it normally finish and return 0, I can get it PID, but is it possible to get the PID of started sshclient?
Upvotes: 2
Views: 1172
Reputation: 51
By default proc.pid
will return the PID of the shell (ie the parent process). What you're looking for is the PID of the child process (sshpass).
So set shell=False
in subprocess.Popen
. Documentation is here.
Upvotes: 1
Reputation: 43024
Just call the sshpass
directly from Popen
and pass in the shell=False
option. Then the direct child will the be the sshpass
subprocess. No need for a shell wrapper.
Upvotes: 0