473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

get the pid of a process with pexpect

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
Jun 27 '08 #1
3 3611
Karim Bernardet <be*******@cppm.in2p3.frwrote:
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 ?
If I understand you correctly what you need to do is run "echo $$" on
the remote shell then "exec tunnel_command". The $$ will print the
pid and the exec will run tunnel_command without changing the pid.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #2
On May 5, 7:18 am, Karim Bernardet <bernar...@cppm.in2p3.frwrote:
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 no**@www.example.com
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 no**@www.example.com &
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 us******@www.example.com

But can you also do something like this?

ssh -N -L 80:localhost:80 us******@www.example.com &
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())

--
Noah
Jun 27 '08 #3
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
us******@xxxxx.fr "'''
.....
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 !
Noah wrote:
On May 5, 7:18 am, Karim Bernardet <bernar...@cppm.in2p3.frwrote:
>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 no**@www.example.com
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 no**@www.example.com &
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 us******@www.example.com

But can you also do something like this?

ssh -N -L 80:localhost:80 us******@www.example.com &
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())

--
Noah
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Surette | last post by:
I have been trying to automate the changing of passwords using python and pexpect. I wrote a script as a test and it works, except that it gives me an exception when it stops running: Exception...
1
by: Baillargeon, Sonny | last post by:
I am trying to use a pexpect script using python v2.3.4 with pexpect module .999 on Solaris 8. I try to execute this script. #!/usr/bin/env python '''This runs "ls -l" on a remote host using...
1
by: Jesse Rosenthal | last post by:
Hello all, I'm writing a script which will backup data from my machine to a server using rsync. It checks to see if I am on the local network. If I am, it runs rsync over ssh to 192.168.2.6...
5
by: funkyj | last post by:
I love pexpect because it means I may never have to use expect again (I don't do any heavy expect lifting -- I just need simple tty control)! As a python advocate I find it embarassing how...
5
by: half.italian | last post by:
Hi all. I try not to post until I am stuck in hole with no way out. I fought with this for several hours, and am currently in the hole. I'm doing a proof of concept for creating afp shares...
1
by: Kevin Erickson | last post by:
Hello, I am attempting to use pexpect in python to copy files from a server using scp; the copy works however exceptions are thrown and it exits unsuccessfully. Below is the a sample code and...
5
by: crybaby | last post by:
I need to ssh into a remote machine and check if mytest.log file is there. I have setup ssh keys to handle login authentications. How do I determine if mytest.log is there by using Pexpect. What...
3
by: Wesley Mesquita | last post by:
Hi all, I am trying to create a test environment to a couple C applications (simple UDP and TCP server/clients), so I want to write this in python and I m looking for ways to do it. Basically I...
4
by: rdabane | last post by:
Hi, I'm trying to perform following operation from inside the python script 1. Open a shell ( start a process ) 2. Send command1 to the process 3. Get output from the process 4. Send command2...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.