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

asked Nov 16, 2019 at 17:50
2
  • 1
    Possible duplicate of Excute python file on start-up Commented Nov 17, 2019 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. Commented Nov 23, 2019 at 15:57

2 Answers 2

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.

answered Nov 17, 2019 at 0:28
4
  • This is a duplicate answer. Commented Nov 17, 2019 at 21:14
  • Do you have a link to the duplicate? Commented Nov 18, 2019 at 1:39
  • duplicates Commented Nov 18, 2019 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.? Commented Nov 18, 2019 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 &
answered Nov 16, 2019 at 18:43
2
  • 1
    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. Commented Nov 17, 2019 at 21:09
  • Ohk. I'll start working on from from now onwards then. Thanks a lot Commented Nov 17, 2019 at 21:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.