1

i am trying to get my WS2812 NeoPixel python sketch to run at boot, i used a tutorial https://tutorials-raspberrypi.com/connect-control-raspberry-pi-ws2812-rgb-led-strips/ it install the correct files and such and all works fine when i run the commands in terminal the lights do as they are meant to, an just trying to figure out how to make them start on boot, i am still really new to all of this

the 3 commands i use to run the python script as follow 

cd rpi_ws281x/

cd python

sudo PYTHONPATH=".:build/lib.linux-armv7l-2.7" python examples/strandtest.py

any help would be much approached

i have tried following a tutorial on hear explaining what to do and it always comes back with an error file or directory not found

  • 1
    Possible duplicate of Excute python file on start-up – Ingo Nov 17 at 21:13
  • Your line with sudo looks wrong. Usually anything all caps with an = should appear first thing on the line (they are environment variables). The corrected line would be PYTHONPATH=".:build/lib.linux-armv7l-2.7" sudo python examples/strandtest.py. – Fred Nov 23 at 15:57
2

You could use cron for this. To schedule a cron job, you will use the crontab utility. Start by reading the man pages for cron and crontab. You could also search the Internet for information resources, and find something similar to this guide.

Once you have the gist (general idea) of this, try this from the command line of your RPi:

crontab -e

This should launch a text editor (nano or pico IIRC) that opens with your existing crontab in it. You will edit this file to add the line you need to start your program at boot time. At the bottom of your existing crontab file, add something like this:

@reboot ( /bin/sleep 30; /usr/bin/python /path/to/your/PythonProgram.py  > /home/pi/cronjoblog 2>&1)

For an explanation of what this command does & why we do it this way, see this answer. If you have any issues getting this to work, please edit your OP, and we'll try to help.


P.S. The crontab Guru is a useful asset to bookmark.

  • This is a duplicate answer. – Ingo Nov 17 at 21:14
  • Do you have a link to the duplicate? – Seamus Nov 18 at 1:39
  • duplicates – Ingo Nov 18 at 1:58
  • @Ingo: Sorry if I'm being thick, but I see no dupes on the link you provided. Let's try it this way: This is one; can you provide the URL of the other - the duplicate in other words.? – Seamus Nov 18 at 15:54
0

You can set a command for your python code to run at every start-up. All that you need is just add a command to rc.local using this command:

sudo nano /etc/rc.local

Add the following command for python2 to the file before exit 0:

sudo python2 /path/to/file.py

And add the following command for python3:

sudo python3 /path/to/file.py

If your code is running with any endless loop like while True, then use ampersand (&) at the end of the command like this:

sudo python3 /path/to/file.py &

  • Please take note that using /etc/rc.local has limitations due to Compatibility with SysV. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it. – Ingo Nov 17 at 21:09
  • Ohk. I'll start working on from from now onwards then. Thanks a lot – theashwanisingla Nov 17 at 21:17

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.