I have a forked child process running which executes a system command

killing the child using os.kill(child_pid, SIGKILL) stops the child process but the system process continues !

Can something be done to stop the system process also?

A part of the code is something like this:
#Child
child_pid = os.fork()
if child_pid == 0:
res = os.system("python abc.py")
os.write(w, str(res))

python abc.py has an infinite loop. I want to stop that after a certain time for which i kill the child but I found out that the execution of the file still continues !!

Please help! Urgent!

Thanks in advance :)

In linux/unix/bsd type OSs, you can chant kill -9 parentProcID from the command line. No clue on Windows OSs

Usually, you would call wait() or some variant after sending the kill signal to the child, then die gracefully yourself (sys.exit(0), maybe; or exit with the exit status of the child; or continue without exit if that is appropriate). See http://docs.python.org/library/os.html#os.wait

The problem is not killing the child.. it is killing the system process initiated by the chils and using wait()... i think.. it only waits till the child process is killed.. it doesnt bother about the system process!

and I am really sorry but the previous command kill -9 parentProcID isnt really clear !
parentProcId is the pid of parent?

Ah. I think we had a small language problem, which you have now fixed.

As I now understand, there is a parent process which spawns a child process which spawns (or in some other way starts) a system process; and you need a way to stop the system process.

The way you do this is that you must know the process ID of the system process, and you use that process ID to send a signal that causes it to stop. Depending on the exact behavior of the system process, you might be able to send a "nice" signal that allows it to clean up before it exits, you you might have to send SIGKILL.

You can do the same thing from within a python script, or on a *nix type system from the command line.
In Python os.kill([I]thePid, someSignal[/I]) From the command line kill [I]-someSignal thePid[/I] In the code for the child, you need to have a signal handler that allows you to first kill the system process before the child stops running. Read here about signal handlers: http://docs.python.org/library/signal.html .

Edited 6 Years Ago by griswolf: parallel args

import os
import signal

processname = 'sysproc'

for line in os.popen('ps xa'):
    if processname in line.split()[4]:
        os.kill(int(line.split()[0]), signal.SIGHUP)
This article has been dead for over six months. Start a new discussion instead.