Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Kill an OS process from script (perhaps unix specific)

Reply
Thread Tools

Kill an OS process from script (perhaps unix specific)

 
 
chengiz@my-deja.com
Guest
Posts: n/a
 
      04-19-2008
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!
 
Reply With Quote
 
 
 
 
Douglas Wells
Guest
Posts: n/a
 
      04-19-2008
In article <(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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- .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl process as a unix background process gbostock@excite.com Perl Misc 14 08-15-2009 02:41 PM
KILL BABY KILL widescreen drsd2kill DVD Video 3 11-29-2004 09:36 PM
Bava's KILL BABY KILL widescreen drsd2kill DVD Video 0 11-27-2004 12:04 AM
Kill a system process within the script Mav Perl Misc 5 06-15-2004 08:49 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments