469,898 Members | 2,166 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

need help to kill a process

Hi

Am new to python and need your help!!

this is my code snip.
within my python script I have the following commands..

<snip>

import os
....
os.system ("cd /home; ./TestTool &")
os.system ("cd /usr/; sh run.sh load.xml &")

<snip>

I need to kill these 2 process after a particular job is done.. is
there any way to get the pids of these process and if so how do i
invoke the kill command through os.system..

or else is there anyother way to do this...

thanks

Feb 7 '07 #1
3 6296
En Tue, 06 Feb 2007 22:59:40 -0300, <el*********@gmail.comescribió:
this is my code snip.
within my python script I have the following commands..

<snip>

import os
...
os.system ("cd /home; ./TestTool &")
os.system ("cd /usr/; sh run.sh load.xml &")

<snip>

I need to kill these 2 process after a particular job is done.. is
there any way to get the pids of these process and if so how do i
invoke the kill command through os.system..

or else is there anyother way to do this...
If you're using Python>=2.4 you could use the subprocess module.

import subprocess
child1 = subprocess.Popen(["./TestTool"], cwd="/home")
child2 = subprocess.Popen(["sh","run.sh","load.xml"], cwd="/usr")

Popen objects have a pid attribute. You don't have to use os.system to
kill them; use os.kill instead.
You'll notice that I leave out the final &, because I don't know how to
start a background process without using the shell. But I think you can
use: bg [pid], afterwards, to get the same result.

--
Gabriel Genellina

Feb 7 '07 #2
On Feb 6, 8:24 pm, "Gabriel Genellina" <gagsl...@yahoo.com.arwrote:
En Tue, 06 Feb 2007 22:59:40 -0300, <elrondru...@gmail.comescribió:


this is my code snip.
within my python script I have the following commands..
<snip>
import os
...
os.system ("cd /home; ./TestTool &")
os.system ("cd /usr/; sh run.sh load.xml &")
<snip>
I need to kill these 2 process after a particular job is done.. is
there any way to get the pids of these process and if so how do i
invoke the kill command through os.system..
or else is there anyother way to do this...

If you're using Python>=2.4 you could use the subprocess module.

import subprocess
child1 = subprocess.Popen(["./TestTool"], cwd="/home")
child2 = subprocess.Popen(["sh","run.sh","load.xml"], cwd="/usr")

Popen objects have a pid attribute. You don't have to use os.system to
kill them; use os.kill instead.
You'll notice that I leave out the final &, because I don't know how to
start a background process without using the shell. But I think you can
use: bg [pid], afterwards, to get the same result.

--
Gabriel Genellina- Hide quoted text -

- Show quoted text -
thx for the reply
os.kill worked fine for the first process.. for the second one the
kill managed to kill the shell but the application is still running..

is there any other work around for this..

thanks

Feb 7 '07 #3
En Wed, 07 Feb 2007 16:50:55 -0300, <el*********@gmail.comescribió:
>import subprocess
child1 = subprocess.Popen(["./TestTool"], cwd="/home")
child2 = subprocess.Popen(["sh","run.sh","load.xml"], cwd="/usr")

Popen objects have a pid attribute. You don't have to use os.system to
kill them; use os.kill instead.
You'll notice that I leave out the final &, because I don't know how to
start a background process without using the shell. But I think you can
use: bg [pid], afterwards, to get the same result.

thx for the reply
os.kill worked fine for the first process.. for the second one the
kill managed to kill the shell but the application is still running..
Don't use the shell, if possible, and replace whatever run.sh does with
python code.

--
Gabriel Genellina

Feb 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Bob Swerdlow | last post: by
10 posts views Thread by Fred | last post: by
10 posts views Thread by Jako Menkveld | last post: by
3 posts views Thread by pattanawadee | last post: by
1 post views Thread by Manfred Braun | last post: by
4 posts views Thread by Richard Rossel | last post: by
2 posts views Thread by =?Utf-8?B?WVhR?= | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.