subprocess Popen: signal not propagating
<p>execute_sleep.py</p>

<pre><code>import os
import time
import subprocess

sleep_script = 'sleep_forever.py'
child = subprocess.Popen(sleep_script,
                         shell=True,
                         close_fds=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT,
                        )

subout = child.communicate()[0]
print subout
rc = child.returncode
</code></pre>

<p>sleep_forever.py</p>

<pre><code>#!/usr/bin/python
import time

while True:
    time.sleep(1)
</code></pre>

<p>When I run execute_sleep.py and then <code>kill -TERM -- -&lt;pid of execute_sleep.py&gt;</code>,
execute_sleep.py dies but sleep_forever.py keeps running.</p>

<p>What can I do so that when execute_sleep.py gets signal, it propagates down to sleep_forever.py?</p>

<p>Do any of these options block signals?</p>

<ul>
<li><code>shell=True</code></li>
<li><code>close_fds=True</code></li>
<li>the use of <code>subprocess.PIPE</code></li>
</ul>

<p>You need to handle the signal, otherwise you are creating orphans (sleep_script processes). So that when the signal to terminate the father process is caught it propagates to child processes before.</p>

<pre><code>def handler(signum, frame):
    functionToKillSleeper(pidtokill)


signal.signal(signal.SIGTERM, handler)
</code></pre>

<h1>Note that SIGKILL cannot be caught.</h1>

<p>So you will need to SIGINT or SIGTERM the father process in order for the signal to be handled, otherwise your problem will remain.  </p>

python subprocess signals