I have a python program named
endless.py. I want to restart it whenever it crashes, and write the time that it restarted, to a file named
restartlog.txt. Here is my python code so far:
import time, os, subprocess while True: x = os.popen('ps -ef | grep python').read() if 'endless.py' not in x: print 'Restarted' y = open('restartlog.txt', 'a') y.seek(0) x = time.asctime( time.localtime(time.time())) z = '\nRestarted at', x z = str(z) y.write(z) print 'Written!' subprocess.call('python endless.py', shell=True) time.sleep(1)
This restarts the program, but it also creates a new one called
/bin/sh -c /usr/bin/python /home/pi/endless.py. It also doesn't write to the file unless I remove the
subprocess.call() (I checked). Is there any way to restart the program cleanly and write it to a file in linux or python?