Now let's see how a Python script can be run in a virtual environment at boot time. This can be done by creating a normal systemd unit file and being careful to use absolute paths.
See this guide for more details about writing and enabling a systemd service:
This example assumes Blinka has been installed on the Pi in a venv named blinka
and that the NeoPixel library has been pip installed into that venv.
Here's an example NeoPixel script that will run a little spin animation on 12 RGBW NeoPixels attached to pin 18 on the Pi's GPIO header.
Save this as neopix_spinner.py in the /home/pi directory.
import time import board import neopixel NEO_PIN = board.D18 NUM_PIXELS = 12 COLOR = 0xADAF00 DELAY = 0.1 pixels = neopixel.NeoPixel(NEO_PIN, NUM_PIXELS, pixel_order="GRBW") while True: for p in range(NUM_PIXELS): pixels.fill(0) pixels[p] = COLOR time.sleep(DELAY)
To access Python installed in the blinka venv, the absolute path is /home/pi/blinka/bin/python3
. The script is saved to /home/pi/neopix_spinner.py
.
Do a test run from the command line to make sure it works:
pi@raspberrypi:~ $ sudo /home/pi/blinka/bin/python3 /home/pi/neopix_spinner.py
This command should not return since the program uses an infinite loop to spin forever. However, if you actually have NeoPixel hardware connected, it should be lighting up. Pressing <CTRL><C>
will break out of the code and return to the prompt.
pi@raspberrypi:~ $ sudo /home/pi/blinka/bin/python3 /home/pi/neopix_spinner.py ^CTraceback (most recent call last): File "/home/pi/neopix_spinner.py", line 16, in <module> time.sleep(DELAY) KeyboardInterrupt pi@raspberrypi:~ $
Now to create and enable a systemd unit file to run this. Use sudo with a text editor to create a file named /lib/systemd/system/neopixel.service
with the following contents:
[Unit] Description=NeoPixel Spinner [Service] ExecStart=/home/pi/blinka/bin/python3 /home/pi/neopix_spinner.py [Install] WantedBy=multi-user.target
Now run the following commands to enable and start the neopixel service:
sudo systemctl enable neopixel sudo systemctl start neopixel
If these commands run as expected, they don't generate much output:
pi@raspberrypi:~ $ sudo systemctl enable neopixel Created symlink /etc/systemd/system/multi-user.target.wants/neopixel.service → /lib/systemd/system/neopixel.service. pi@raspberrypi:~ $ sudo systemctl start neopixel pi@raspberrypi:~ $
But the NeoPixels should be lighting up now. Further, this process should run everytime the Pi is booted.
If you want to stop the script from running at boot, then stop (stops current execution) and disable (removes sym link used to boot at start) the service:
sudo systemctl stop neopixel sudo systemctl disable neopixel
The status can be checked using status.
pi@raspberrypi:~ $ sudo systemctl status neopixel ● neopixel.service - NeoPixel Spinner Loaded: loaded (/lib/systemd/system/neopixel.service; enabled; preset: enabled) Active: active (running) since Mon 2023-10-23 16:58:53 PDT; 1s ago Main PID: 900 (python3) Tasks: 1 (limit: 1586) CPU: 124ms CGroup: /system.slice/neopixel.service └─900 /home/pi/blinka/bin/python3 /home/pi/neopix_spinner.py Oct 23 16:58:53 raspberrypi systemd[1]: Started neopixel.service - NeoPixel Spinner. pi@raspberrypi:~ $
Text editor powered by tinymce.