need help to kill a process

Discussion in 'Python' started by elrondrules@gmail.com, Feb 7, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. En Tue, 06 Feb 2007 22:59:40 -0300, <> escribió:

    > 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
     
    Gabriel Genellina, Feb 7, 2007
    #2
    1. Advertising

  3. Guest

    On Feb 6, 8:24 pm, "Gabriel Genellina" <> wrote:
    > En Tue, 06 Feb 2007 22:59:40 -0300, <> escribió:
    >
    >
    >
    >
    >
    > > 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, 2007
    #3
  4. En Wed, 07 Feb 2007 16:50:55 -0300, <> escribió:

    >> 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
     
    Gabriel Genellina, Feb 8, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlo Filippini

    Kill win32 process

    Carlo Filippini, Jan 7, 2004, in forum: Perl
    Replies:
    0
    Views:
    1,135
    Carlo Filippini
    Jan 7, 2004
  2. Brian
    Replies:
    1
    Views:
    800
    Mothra
    May 25, 2004
  3. Donna

    Kill asp_wp process

    Donna, Dec 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    606
    Alvin Bruney
    Dec 10, 2003
  4. Michael Tkachev

    How can I kill an "Excel" process from a memory?

    Michael Tkachev, Oct 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,308
    =?Utf-8?B?RXRoZW0gQXp1bg==?=
    Oct 18, 2004
  5. charmis

    cannot kill excel process

    charmis, Jan 11, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,431
    Elton Wang
    Jan 11, 2005
Loading...

Share This Page