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
2 Answers 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.
-
-
Do you have a link to the duplicate?Seamus– Seamus2019年11月18日 01:39:35 +00:00Commented Nov 18, 2019 at 1:39
-
-
@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– Seamus2019年11月18日 15:54:25 +00:00Commented Nov 18, 2019 at 15:54
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 &
-
1Please 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– Ingo2019年11月17日 21:09:32 +00:00Commented Nov 17, 2019 at 21:09 -
Ohk. I'll start working on from from now onwards then. Thanks a lottheashwanisingla– theashwanisingla2019年11月17日 21:17:41 +00:00Commented Nov 17, 2019 at 21:17
sudo
looks wrong. Usually anything all caps with an = should appear first thing on the line (they are environment variables). The corrected line would bePYTHONPATH=".:build/lib.linux-armv7l-2.7" sudo python examples/strandtest.py
.