get the pid of a process with pexpect

  • Thread starter Karim Bernardet
  • Start date
K

Karim Bernardet

Hi

I am using pexpect to do ssh tunneling and to open a vnc server (jobs on
a grid cluster). When the job is canceled, these 2 processes remain on
the worker node (they are detached), so I have to kill them (using a
trap command in the bash script of the job) but I need the pid of each
process. I have tried to get it like this

ssh_tunnel = pexpect.spawn (tunnel_command % globals())
....
print ssh_tunnel.pid

but ssh_tunnel is not the pid of the ssh tunnel

Is there a way to get it using pexpect ?

Cheers

Karim
 
N

Noah

ssh_tunnel = pexpect.spawn (tunnel_command % globals())
...
print ssh_tunnel.pid

but ssh_tunnel is not the pid of the ssh tunnel

Is there a way to get it using pexpect ?

You will notice that you can't get this information even from the
shell. This does not work of course:

ssh -f -N -L 81:localhost:80 (e-mail address removed)
echo $!

However, this seems to work, but I don't trust it. Certainly it isn't
a real daemon, but this work OK for you if you only need to manage the
tunnel for the duration of your script. Notice that now Bash had the
PID in $! variable:

ssh -N -L 81:localhost:80 (e-mail address removed) &
TUNNEL_PID=$!
echo $TUNNEL_PID

What command-line are you using for 'tunnel_command'? This is hard
because SSH does not provide a way to get the PID of the tunnel if you
request ssh to go to the background (see the -f option). I always
considered this a bug because it makes scripting hard. Even if you
start the tunnel from a shell you can't use $! to get the PID because
the daemonizing is initiated by ssh. This is not the same use using
the shell to put a command into the background, so the shell won't
know anything about the PID.

I'm not sure if you can put ssh into the background using the shell
and still have the tunnel work. So you might start a tunnel something
like this:

ssh -f -N -L 80:localhost:80 (e-mail address removed)

But can you also do something like this?

ssh -N -L 80:localhost:80 (e-mail address removed) &
echo $!

And for that to even work you will have to use Pexpect to start bash.
Remember, Python doesn't start your command in a subshell, so you have
to specify it if you want. So your tunnel command would have to be
something like this:

tunnel_command = '''bash -c "ssh -N -L ...foo... &"'''
ssh_tunnel = pexpect.spawn (tunnel_command % globals())
 
K

Karim Bernardet

Many thanks !
This is what I did

for mytunnel.py :
I use pexpect like this
tunnel_command = '''bash -c "ssh -N -R 60011:localhost:60002
(e-mail address removed) "'''
.....
com="echo \""+str(ssh_tunnel.pid)+"\" >> pids"
....
time.sleep(172800)

in the bash script which calls mytunnel.py :
../mytunnel.py ${USERN} ${HMM} ${P2USE} ${p2use2} &
echo $! >>pids

this way I have all the pids

Cheers !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,043
Members
48,769
Latest member
Clifft

Latest Threads

Top