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