I put a python script with an infinite loop into
/etc/rc.local but the machine boots successfully, which confuses me.
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. # Print the IP address _IP=$(hostname -I) || true if [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP" fi /home/pi/py/startsignal.py & /home/pi/py/fan.py touch /home/pi/thisisrun exit 0
#!/usr/bin/python import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) GPIO.output(18, 1)
#!/usr/bin/python # coding: utf8 import RPi.GPIO as gpio gpio.setmode(gpio.BCM) upper_temp = 55 lower_temp = 45 # minutes check_interval = 2 def get_temp(): with open('/sys/class/thermal/thermal_zone0/temp', 'r') as f: temp = float(f.read()) / 1000 return temp def check_temp(): if get_temp() > upper_temp: gpio.setup(23, gpio.OUT) elif get_temp() < lower_temp: gpio.setup(23, gpio.IN) if __name__ == '__main__': # check every 2 minutes try: while True: check_temp() sleep(check_interval * 60) finally: gpio.cleanup()
All the code relevant is above. I thought about this after googling around.
#!/bin/sh -eindicates that the script will exit once an error occurs.
/home/pi/thisisrunfile is not created, so there must be an error above this line
- after booting into system I can see that
fan.pyis running. So I guess the error occurs during the execution of
fan.py. But the
fan.pyhas an infinite loop in it !
How can a python script generates an error but still runs normally?
/bin/sh detects the error when the
fan.py never returns?
OS: raspbian stretch