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()

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)

        data = [measure_pi_temp(), temperature, humidity, get_time_now(), get_date()]

        with open(path, 'a') as output:
            writer = csv.writer(output, delimiter=',')

if __name__ == '__main__':
    print('Program is starting...')
    except KeyboardInterrupt:
  • @joan would like to see the entire script. Wasn't sure if it was relevant since the script runs just fine. I can add it if that helps. Sep 11, 2019 at 16:47
  • I expect it's a path type problem. Perhaps see if it works when you add @reboot /usr/bin/python /home/pi/pi_projects/temp/temp.py to your user crontab (cron -e) and reboot.
    – joan
    Sep 11, 2019 at 16:54
  • I could not install the gnome-schedule. Keep getting dpkg error no matter what I do so thats why I opted for this option. Sep 11, 2019 at 17:22
  • Its saying cron: invalid option - - 'e' Sep 11, 2019 at 17:26
  • 1
    You should add some debug logging to the script so you can determine where/when things go wrong. Note that using sudo in rc.local is at best pointless, it is run with root privileges at boot.
    – goldilocks
    Sep 11, 2019 at 17:44

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? Sep 11, 2019 at 19:03
  • /usr/bin/python /home/pi/pi_projects/temp/temp.py & Sep 12, 2019 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.