473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1510
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bob Swerdlow | last post by:
My application starts up a number of processes for various purposes using: self.popen = popen2.Popen3("/usr/local/bin/python -O "myscript.py") and then shuts them down when appropriate with...
10
by: Fred | last post by:
There is a setting in INIT.ORA that has the unintended side-effect of making sure the ALTER SYSTEM KILL SESSION command has immediate affect. Without this setting, I've seen some instances where...
2
by: Micky | last post by:
Can any one give me some pointers on how to kill a running procedure in DB2? I have a long running procedure called from a Java routine and after I kill the Java routine, the procedure keeps...
3
by: pattanawadee | last post by:
Deall All, Could anybody suggestion me How to kill all inherrit processes (sibling child,previous and parent process) in the case I know only child process id and user id, For example I strart...
8
by: MackS | last post by:
Hello! This question does not concern programming in python, but how to manage python processes. Is there a way to "name" a python process? At least on Linux, if I have two python programs...
2
by: Poly-poly man | last post by:
I am working on a GTK Midi player (timidgtk.sourceforge.net). As you can see, it's just a timidity frontend. With version 0.03, I'm trying to devel it to use SDL_sound to play the midis. First...
4
by: Richard Rossel | last post by:
Hi Fellows, I have a problem with process termination. I have a python code that apache runs through a django interface. The code is very simple, first, it creates a process with the...
5
by: Mirxon | last post by:
Hello, I'm working on a C program under Ubuntu. It's basd on socket. Browser calls a client cgi (C program), and send some parameters to server (C program). Server runs another program...
4
by: Ashit Vora | last post by:
Hi, My query is... I have a parent process which forks a child process to perform certain task. I wish to terminate the child process after certain amount of time (say 60 secs). Since I dont...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.