killing children spawned by pexpect

L

loorthu

I am noticing that pexpect kills any child that it is spawned when the
parent is terminated using SIGINT (e.g Ctrl-C on the shell), but not
when it is killed by SIGKILL (e.g 'kill -9' on the parent process).
Here is a quick demo (tried on Linux 2.6.9 kernel):

#!/usr/local/bin/python


import pexpect, re


cmd = “<any time consuming shell command here>”


child = pexpect.spawn(cmd)

cpl = child.compile_pattern_list([pexpect.EOF, re.compile("(.*?)\r
\n")])

while True:

r = child.expect_list(cpl)

if r == 0: break

line = child.match.group(1)

print line

child.close()

print 'Exit ', child.exitstatus



Now, if I kill the python process using Ctrl-C (SIGINT), it kills the
child process spawned as well. But if it is killed by SIGKILL (by
doing a ‘kill -9’), the parent python exits, but the child goes on…
What is the best way to ensure that all children are killed when the
parent who spawned them terminates?



Thanks,

Cottalango
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top