469,336 Members | 5,570 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Kill an OS process from script (perhaps unix specific)

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!
Jun 27 '08 #1
1 1420
In article <7c**********************************@a22g2000hsc. googlegroups.com>,
ch*****@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- .
Jun 27 '08 #2

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
2 posts views Thread by Micky | last post: by
3 posts views Thread by pattanawadee | last post: by
8 posts views Thread by MackS | last post: by
2 posts views Thread by Poly-poly man | last post: by
4 posts views Thread by Richard Rossel | last post: by
5 posts views Thread by Mirxon | last post: by
4 posts views Thread by Ashit Vora | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.