Kill an OS process from script (perhaps unix specific)

Douglas Wells see at signature.invalid
Sat Apr 19 13:49:42 EDT 2008


In article <7cd7208f-777b-4264-8d34-3c4bf3377940 at a22g2000hsc.googlegroups.com>,
  chengiz at my-deja.com 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-                     .



More information about the Python-list mailing list