Python程序在某个时候停止,Beaglebone黑色Debian

Python program stops after sometime, Beaglebone black Debian

The program runs on beaglebone black (rev.C) debian using command prompt (PuTTY) seems to work for some 1-2h max then stops (=no data log) Tested with few minutes and it works fine but I have no idea why it would stop after a while even though The BBB is still running (at stable power supply)

Btw, any comment is appreciated. Any improvement recommendation is of great help. It's clearly my first program ever

Update 01: full program code sample of latest behaviour: run 1h logging, 9 times. it stops logging after 2 hours (almost exact 2h) and that file yeilds "last edited" 2h after (e.g. stop logging at 3.00, last edited at 5.00)

import Adafruit_BBIO.ADC as ADC
import Adafruit_BBIO.GPIO as GPIO
import time
import sys
sensor_00 = 'P9_33'
sensor_mk = 'P9_35'
pinLED = 'P9_41'
ADC.setup()
GPIO.setup(pinLED, GPIO.OUT)

print('Sensor reading test..........')+ "\n"
test_rw00 = ADC.read_raw(sensor_00)
test_rwmk = ADC.read_raw(sensor_mk)
print('Sensor 00: %s' %test_rw00) + "\n"
print('Sensor mk: %s'%test_rwmk )+ "\n"

LogTime = int(input("Data file logging interval (seconds):"))
delay = int(raw_input("Reading delay: "))
TestLocation = raw_input("Test location:")
TestSpecs = raw_input("Test scpecs/ purposes: ")
n = int(raw_input("Estimated amount of Data file:"))

def indicatingLED():
    GPIO.output(pinLED, GPIO.HIGH)
    time.sleep(3)
    GPIO.output(pinLED, GPIO.LOW)
    time.sleep(1)

def TestSpecs_txtWrite():
    datafile.write("Date and time of measurement:%s" % log_date_str+ "\n")
    datafile.write("Data file logging interval (seconds): %s" %LogTime + "\n")
    datafile.write("Test Location: %s" %TestLocation + "\n")
    datafile.write("Test specs/ purposes: %s" %TestSpecs + "\n")
    datafile.write("Test readings, sensors in position, elevator doesn't run: Sensor00: " + str(test_rw00) + ",    ")
    datafile.write("Sensor mk: " + str(test_rwmk) + "\n")
    datafile.write("-------------------------------------------------------"+"\n")


print('Sensor reading started...')+ "\n"

for i in xrange(1,n):
  t_end = time.time() + LogTime
  log_date_str = time.strftime("%Y%m%d_%H%M")
  datafile = open("%s.txt" %log_date_str,"w+")
  TestSpecs_txtWrite()
while time.time() < t_end:
  indicatingLED()
  val_rw_00 = ADC.read_raw(sensor_00)
  val_rw_mk = ADC.read_raw(sensor_mk)
  datafile.write("Vol_Sensor00:" + str(val_rw_00) +",     ")
  datafile.write("Vol_Sensormk:" + str(val_rw_mk)+"\n")
  time.sleep(delay)
log_date_str = time.strftime("%Y%m%d_%H%M")
datafile.write("Log ended at: %s" %log_date_str + "\n")
print('Measurement ended !-----------------------------------------------')

As you say you are disconnecting from the Board. This will after a connection timeout lead to the connection being reset and the program losing its controlling TTY. This will ultimately terminate the program and is expected behavior.

You should explore things like:

  • Append a & to make the process detach from the TTY
  • Look into nohup
  • Run it inside a terminal multiplexer like screen or tmux
  • Have the program itself go into 'background' by detaching itself from the controlling terminal.

There are still others like rc.local or a systemd service file. The above should be plenty enough of starting points for a simple solution though. A good book on basic Linux or embedded Linux systems would help too.

I'd recommend screen or tmux in this case. There is plenty of documentation on how to use them.
testing tmux atm. Thanks a lot TBR
Thanks for the root cause indication. Problem with '&' is I have to give inputs to the program after running --> yeilds -bash: ... command not found.... Should I alter my code in order to have it run in the background or try other ways