How to monitor a python process and restart it upon abnormal termination
<p>Assume that there is a <code>task.py</code>,breaking due to memory overflow.How can i monitor this and restart it?</p>

<pre><code>import time
while(1):
    print('.')
    # simulate breaks
    time.sleep(2)
    exit(0)
</code></pre>

<p>Thanks</p>

<p>You can use a watchdog.
Make your worker process update a dummyfile say every 10 secs.
Have another, completely independent, process check if the last access wasn't longer that say 20 secs ago.
If it was, restart your worker process.</p>

<p>There are all kinds of nifty OS-dependent ways to do the same, but this low-tech one always works, even trivially over a network, assuming your clocks are synchronized.</p>

python linux bash