I cannot seem to get any of the options out there to work. I am trying to auto start a py file when the Raspberry Pi boots up. I need it to stay open and running continuously as it detects knock sensors. I can run the file perfectly when the RPi boots up and I type:

sudo python -i /var/www/html/liveSensor.py

into the terminal. I tried adding:

@reboot sudo python -i /var/www/html/liveSensor.py

to cron but it still doesn't work. I have seen people have success adding the file to the pi/.config/ directory, but that directory does not exist on my RPi. I have an RPi 3 B running Raspbian.

What is the best way to accomplish this?

  • You cannot enter interactive mode on startup. What does the program do? – Milliways May 5 '16 at 3:51
  • The program detects 2 knock sensors, eachtime the sensor gets knocked it adds a value to a sqlite db, then i use php and ajax to retrieve and display that data in my browser on a apache local server..everything works great I just want to try and skip the step of manually starting the py script. What would you suggest @Milliways ? – Ryan D May 5 '16 at 3:54
  • Why the "-i"??? – Milliways May 5 '16 at 3:56
  • The -i makes it stay running, if i dont have it the script runs once and stops @Milliways – Ryan D May 5 '16 at 3:58

You need to drop the "-i". What is it supposed to interact with?

If you want the script to keep running you need to write one that loops and keeps running. If it only runs once at the command line (without the "-i") that is what it will do on startup.

  • Ok I will post my script to another question and ask how to keep it running as I am new to python.. – Ryan D May 5 '16 at 4:05

Init Script

If you want a program to run at boot and stay running, you probably want an Init Script.

This might help get you started...

Copy the following into /etc/init.d/liveSensor:

case "$1" in
  echo "Starting liveSensor.py."
  echo "Stopping liveSensor.py."
  pkill liveSensor.py # totally dangerous.  Come up with a nice way to kill your program.
  echo "Usage: $0 {start|stop}"
  exit 1
exit 0

Create a symbolic link to your init script in the correct rc.d directory:
(Replace the number in rc5.d with whatever running the runlevel command returns.)
(Default for 2016-03-18-raspbian-lite is 5.)

ln -s /etc/init.d/liveSensor /etc/rc5.d/S05liveSensor

Also, if you haven't already, add the following line as the first line in liveSensor.py:
(This tells Linux to run your script using python.)

#!/usr/bin/env python

Also make sure that you set permissions correctly:

chmod +x /var/www/html/liveSensor.py
chmod +x /etc/init.d/liveSensor

Your script isn't supposed to be interactive, right?

  • 1
    First this will not solve the problem, which is a faulty script. Secondly you should NOT write init.d scripts for Jessie which runs systemd but write a systemd service . systemd will attempt to convert a well structured init.d script (which your example is NOT) into a service but it is better to write the service to do the task. Thirdly cron is probably better suited to the task proposed. – Milliways May 5 '16 at 4:30
  • Firstly you are correct: something's up with the Python script if it kills itself when run, instead of waiting around, listening for something like it's supposed to. Secondly, the (simple, but not LSB compliant) init script works with systemd. I was not assuming that he was running Jessie. A systemd service unit file won't work with sysvinit. Thirdly, why is cron more appropriate than init for keeping a program always running in the background? Isn't that the purpose of init, while cron is for scheduled, short-running jobs? – Hydraxan14 May 5 '16 at 16:46

So all I needed was to make the python script an infinite loop. Now it all works; thanks guys for the help!

I added this to the loop:

while 1 == 1:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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