I have a simple script will runs a loop that reads off temp data in the console from a sensor and then writes it to a file. I would like to make sure that this script starts running right after my pi is rebooted. To keep the data flowing after a power outage and etc. I am running on Pi 3, Debian v9.11, Raspbian GNU/Linux 9, Python 3. Below is what I am using:
I decided to go for rc.local method since I am planning to run this pi in headless mode after I get everything tested. Right before the exit 0 line of the file I have the following line of code:
sudo python /home/pi/pi_projects/temp/temp.py &
When I reboot the script does not start and I have to run it manually. After some research, I saw that some folks resolved this by changing the script file permissions to 755. I have tried that as well but same result.
Below is the script I am trying to run on reboot:
#!/usr/bin/env python
import RPi.GPIO as GPIO
import Adafruit_DHT as DHT
import lcddriver
import time
from datetime import datetime, date
import csv
import sys
import os
sensor = DHT.DHT11
pin = 17
path = 'data.csv'
lcd = lcddriver.lcd()
lcd.lcd_clear()
def get_time_now(): # get system time
return datetime.now().strftime('%H:%M:%S')
def get_date(): # get system time
return date.today()
def measure_pi_temp():
temp = os.popen("vcgencmd measure_temp").readline()
temp = temp.replace("temp=","")
temp = temp.replace("'C","")
return (float(temp) * 9/5.0 + 32)
def loop():
while (True):
humidity, temperature = DHT.read_retry(sensor, pin)
#if humidity is not None and temperature is not None:
if humidity is not None and humidity < 101:
humidity = round(humidity, 2)
temperature = round((temperature * 9/5.0 + 32), 2)
print('Pi Temp: {4:0.1f}F Room Temp: {0:0.1f}F Humidity: {1:0.1f}% Time: {2} Date: {3}'.format(temperature, humidity, get_time_now(), get_date(), measure_pi_temp()))
lcd.lcd_display_string('T:' + str(temperature) + 'F ' + 'H:' + str(humidity) + '%', 1)
lcd.lcd_display_string('Time: ' + get_time_now(), 2)
else:
print('none')
data = [measure_pi_temp(), temperature, humidity, get_time_now(), get_date()]
with open(path, 'a') as output:
writer = csv.writer(output, delimiter=',')
writer.writerow(data)
time.sleep(30)
if __name__ == '__main__':
print('Program is starting...')
try:
loop()
except KeyboardInterrupt:
GPIO.cleanup()
exit()
1 Answer 1
rc.local
runs with an empty environment, so you need to use absolute paths of all commands that you call. Currently you execute sudo
with python /home/pi/pi_projects/temp/temp.py
as arguments. Since you don't specify the full path of the executable you run (that is, /usr/bin/sudo
), your command cannot be started.
Notably, rc.local
is executed with root permissions, so you don't need to put sudo
there at all. You should give your script as an argument to /usr/bin/python
(note the full path).
-
Thanks. I am new to this. Can you tell me exactly what I should add to the rc.local?user3088202– user30882022019年09月11日 19:03:27 +00:00Commented Sep 11, 2019 at 19:03
-
/usr/bin/python /home/pi/pi_projects/temp/temp.py &
Dmitry Grigoryev– Dmitry Grigoryev2019年09月12日 08:05:48 +00:00Commented Sep 12, 2019 at 8:05
@reboot /usr/bin/python /home/pi/pi_projects/temp/temp.py
to your user crontab (cron -e
) and reboot.sudo
inrc.local
is at best pointless, it is run with root privileges at boot.