Kill an OS process from script (perhaps unix specific)

Discussion in 'Python' started by chengiz@my-deja.com, Apr 19, 2008.

  1. Guest

    Hi,
    I'm trying to run a process from a python script. I need the exit
    status of that process but do not care about its output, so until now
    was using os.system(). But it turned out that the process often went
    into an infinite loop, so I wrote a SIGALRM handler. Unfortunately the
    code I came up with is quite kludgy:

    import subprocess
    ....
    try:
    p = subprocess.Popen(..., shell = True)
    pid = p.pid
    os.waitpid(pid...)
    ...
    except ...: # Thrown by alarm signal handler
    os.kill(pid + 1) # "Real" pid = shell pid + 1
    ...

    The os.kill is very hacky and unsafe so I was looking for better
    ideas. Any help will be greatly appreciated. Thanks!
    , Apr 19, 2008
    #1
    1. Advertising

  2. In article <>,
    writes:
    > Hi,
    > I'm trying to run a process from a python script. I need the exit
    > status of that process but do not care about its output, so until now
    > was using os.system(). But it turned out that the process often went
    > into an infinite loop, so I wrote a SIGALRM handler. Unfortunately the
    > code I came up with is quite kludgy:
    >
    > import subprocess
    > ...
    > try:
    > p = subprocess.Popen(..., shell = True)
    > pid = p.pid
    > os.waitpid(pid...)
    > ...
    > except ...: # Thrown by alarm signal handler
    > os.kill(pid + 1) # "Real" pid = shell pid + 1
    > ...
    >
    > The os.kill is very hacky and unsafe so I was looking for better
    > ideas. Any help will be greatly appreciated. Thanks!


    Assuming that the problem is really an infinite loop (and not just
    an arbitrary delay), you could use the simple construct:

    import os
    code = os.system ("ulimit -t <secs> ; ...")

    That's not guaranteed to work on all POSIX systems, but it should
    work with at least ash, bash, and ksh. And it would would be
    "limit cputime <secs> ; ..." if you somehow got hooked up with a
    C shell.

    - dmw

    --
    .. Douglas Wells . Connection Technologies .
    .. Internet: -sp9804- -at - contek.com- .
    Douglas Wells, Apr 19, 2008
    #2
    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,087
    Carlo Filippini
    Jan 7, 2004
  2. Brian
    Replies:
    1
    Views:
    761
    Mothra
    May 25, 2004
  3. Toby Inkster

    Useful script perhaps

    Toby Inkster, Aug 27, 2005, in forum: HTML
    Replies:
    0
    Views:
    354
    Toby Inkster
    Aug 27, 2005
  4. Mav
    Replies:
    5
    Views:
    115
    Mothra
    Jun 15, 2004
  5. Replies:
    14
    Views:
    245
    Martijn Lievaart
    Aug 15, 2009
Loading...

Share This Page