I run a Python program in a screen session which is started from my /etc/rc.local with this command:
su me -c "screen -d -m /usr/bin/python /mnt/usbdrive/pgms/mypgm.py"
Recently my program has been trapping an end of file situation which I have not yet figured out, but for that situation it would normally terminate with an EOF message. Since it is in a screen session, when it terminates the screen session also closes so I also miss any error messages on the screen but do have those which I write to a file.
Usually when this happens I can reboot or just manually restart the screen session and the program. It will run normally for hours, days, weeks,... or until the next EOF
While I'm trying to figure out the cause, I decided to implement a fix such that if the EOF situation is detected it saves a counter to a file and reboots:
subprocess.call(["sudo", "shutdown", "-r", "now"]). If the program runs normally for a period that counter gets set back to 0, but if it continues to get the EOF error then after the 3-rd error in a row, I run the following code:
if int(data) > 3: print "More than 3 EOF's in a row. Forcing a restart but without starting the Python Pgm" pi = open('/mnt/usbdrive/Output/error.txt','a') wrtline="\r\n*****" + " More than 3 EOF's in a row. Forcing a restart but without starting the Python Pgm" + time.strftime("%Y-%m-%d %H:%M:%S") + "\n" pi.write (wrtline) pi.close() shutil.move('/etc/rc.local', '/etc/rc.local.withpgm') shutil.move('/etc/rc.local.nopgm', '/etc/rc.local') subprocess.call(["sudo", "shutdown", "-r", "now"])
/etc/rc.local.nopgm is identical to the original
rc.local except I have pound-ed out the line which starts the screen session.
OK, it works!!!!!
Except, when I go in via PUTTY using my id (me) and then
sudo nano /etc/rc.local I see the original rc.local and not the one with the line pounded-out! If I go in via the console and look at the
/etc/rc.local it has that line pounded out!
What's going on? It would appear that some how I have created two
/etc/rc.local files. The changed one is run at bootup. The code with the
shutil.move lines is being executed in the Python program in the screen session before the reboot.