need help to kill a process

E

elrondrules

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
 
G

Gabriel Genellina

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.
 
E

elrondrules

En Tue, 06 Feb 2007 22:59:40 -0300, <[email protected]> escribió:




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

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

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.

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
 
G

Gabriel Genellina

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.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top