Marselo Bonagi
Marselo Bonagi

Reputation: 173

get pid of process in subprocess python + bash

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

Answers (3)

oliver
oliver

Reputation: 2833

Using subprocess.Popen I would simply call the shell=false option.

Upvotes: 0

staples
staples

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

Keith
Keith

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

Related Questions