I began this answer before @ben_nuttall answered. After reading @ben's answer, I feel part of my answer (below) may still add something. However, I feel @ben's answer on starting the
pigpiod service is the best and simplest. For completeness, here is @ben's answer for starting
sudo systemctl enable pigpiod
pigpiod is started such that it becomes part of the normal boot sequence on your RPi, following is an approach for running your Python script at boot time.
Start Python script at boot time using
There are several ways to do this. IMHO, the two "best" ways are 1)
systemd, and 2)
cron. The tradeoffs (again, IMHO) are that
systemd is more comprehensive and technically superior, but more difficult (which only means that you'll need to invest more time learning before you can do useful things with it). I feel that
cron is easier, so that's the answer I'll propose below:
- From the command line, type
Read the manual (man page) for
cron. It's short, and you should have the background.
cron are scheduled from a file that's called the
crontab. You'll edit this file to add your "job". There's also a manual for
crontab if you care to read it (you should):
Start the editor for
crontab, and enter the instructions for starting your "job" on the command line of a terminal as follows:
(NOTE: I've added the
$ symbol at the beginning of an input as shorthand for the
bash shell prompt)
$ crontab -e
Your default editor (likely
nano) will start, displaying the default contents of your
crontab. Move the cursor to the beginning of the first empty line at the bottom of the editor's display. Enter the following line:
@reboot /usr/bin/python3 /home/pi/moving/moving_test.py > /home/pi/cronjoblog 2>&1
^O), then exit the editor (
^X) to return to the shell/command line.
Note the use of complete file specifications in this entry. This is due to the fact that your
crontab is not executed with the same $PATH that you will have as user
pi. It's therefore a good habit to get into, even if it's not always strictly necessary. Note also the redirection of any
stderr (error messages) to the file
/home/pi/cronjoblog preserves this useful information for any post-op debugging that may be needed. Again, good habit.
- Reboot your RPi, and check if the Python script executed. If it did not, inspect the file
/home/pi/cronjoblog for error messages that will help isolate the problem.
Occasionally, you will find that a job you've scheduled in your
crontab will persistently fail to execute, and the cause will be unclear. The issue may be due to the fact that
cron does not maintain awareness of whether programs and services have actually booted before it calls them. This is typically remedied by adding a brief
sleep delay to give these services time to boot before they are called. The following adds a 10 second delay to the previous line:
@reboot /bin/sleep 10; /usr/bin/python3 /home/pi/moving/moving_test.py > /home/pi/cronjoblog 2>&1
cron's lack of awareness is a shortcoming, and the primary reason that
systemd is a technically superior approach. Nevertheless,
cron remains useful and simple. Try this, and let us know how you get on via comments, or by editing your original question.