Terminating a program within a time frame through python
<p>I'm running a fortran code from a python script which sometimes takes a while to run. Thus I'm limiting the run time with a code taken from <a href="http://stackoverflow.com/a/13821695/3910261">this link</a>:</p>

<pre><code>def timeout(func, args=(), kwargs={}, timeout_duration=15, default=1):
    import signal

    class TimeoutError(Exception):
        pass

    def handler(signum, frame):
        raise TimeoutError()

    # set the timeout handler
    signal.signal(signal.SIGALRM, handler) 
    signal.alarm(timeout_duration)
    try:
        result = func(*args, **kwargs)
    except TimeoutError as exc:
        result = default
    finally:
        signal.alarm(0)

    return result
</code></pre>

<p>I'm basically putting another function (partly below) in to this one (above) to run fortran code as:</p>

<pre><code>subprocess.check_output('./../bin/SPhenoUMSSM ../UMSSM/LH_out_'+mod+' &gt; SPheno_log_'+mod, shell=True)
</code></pre>

<p>However I realised that when fortran code takes more than 15 seconds, which is the boundary in timeout function, it leaves in the core and executes other one in the for loop which creates dump in my cores. In order to prevent that, I wanted to use <code>subprocess.popen()</code> since it gives me pid to terminate the job in the core, but I need to wait for the process to be executed as well, like <code>subprocess.check_output()</code> does. Thus I was wondaring if there is a way to combine popen and check_output properties to wait until job is done within 15 seconds and if its not just terminates that.</p>

<p>Not the most sophisticated piece of code in the world but it may be useful.</p>

<pre><code>import subprocess, time
x = subprocess.Popen(['sleep', '15'])
polling = None
i = 0
while polling == None:
    time.sleep(1)
    polling = x.poll()
    i +=1
    if i &gt; 15: break
if polling == None:
    try:
        x.kill()
        print "Time out - process terminated" # process terminated by kill command
    except OSError:
        print "Process completed on time" # process terminated between poll and kill commands
    except Exception as e:
        print "Error "+str(e) # kill command failed due to another exception "e"
else:
    print "Process Completed after "+str(i)+" seconds"
</code></pre>

<p>Edit: Problems with kill not appearing to function.<br>
Try using <code>os.kill(x.pid, signal.SIGKILL)</code> rather than <code>SIGTERM</code>.<br>
I believe that <code>SIGTERM</code> asks the process to close down cleanly, rather than terminate immediately. Not knowing what drives the fortran script, it's difficult to know what the terminate signal does. Perhaps the code is doing something.<br>
For example:<br>
if I ran a shell script as follows:    </p>

<pre><code>#!/bin/bash
trap "echo signal" 15
sleep 30
</code></pre>

<p>and sent it <code>kill -15 pid_number</code>, it would not print "signal" until the sleep had terminated after 30 seconds, whereas if I issued <code>kill -9 pid_number</code> it would terminate immediately with nothing printed out.   </p>

<p>The short answer, is I don't know but I suspect that the answer lies within the script running the fortran code.</p>

<p>EDIT:</p>

<p>Note: In order to successfully run <code>x.kill()</code> or <code>os.kill()</code> or <code>subprocess.call('kill '+ str(x.pid), shell=True)</code>, <code>shell</code> option in x needs to be False. Thus one can use </p>

<pre><code>import shlex
args = shlex.split(ARGS HERE) 
x = subprocess.Popen(args) # shell=False is default
</code></pre>

<p>But also note that if you want to write the output to a log file by using <code>... &gt;&amp; log_file</code> it wont work since <code>&gt;&amp;</code> is not an valid argument for your script but for your shell environment. Thus one needs to use only arguments that are valid for the script that python runs.</p>

python subprocess popen