Raspberry Pi Asked on December 4, 2021
I have a code set up to run at boot, and it works fine when I start it via terminal, but it doesn’t run at boot. I opened the terminal, and this is what I saw:
Running at boot
Traceback (most recent call last):
File "/home/pi/program.py", line 2, in <module>
import wiringpi
ImportError: No module named wiringpi
I know I have it on the pi as it works when I start it manually, but for some reason it doesn’t work at boot. Does anybody know how to fix this?
An alternative to systemd
for starting a program at boot is cron
. If you've not used cron
to schedule jobs, you may find it easier than systemd
. Here's how:
cron
- the crontab
:$ crontab -e
This will open your crontab
in the editor you have chosen (nano
is a good choice)
crontab
- add the following line in the editor:@reboot ( /bin/sleep 15; /home/pi/program.py >> /home/pi/cronjoblog 2>&1 )
/home/pi/cronjoblog
Answered by Seamus on December 4, 2021
You cannot use ~/.bashrc
to run a program at boot up. It is to configure bash
and runs only with bash sessions and not at boot up of the RasPi. To have a service started on boot up you best use a systemd Unit file. Try it by creating a Unit file with:
rpi ~$ sudo systemctl --force --full edit myprogram.service
In the empty editor insert these statements, save them and quit the editor:
[Unit]
Description=My python3 script
After=multi-user.target
[Service]
User=pi
WorkingDirectory=/home/pi
ExecStart=/usr/bin/python3 /home/pi/program.py
[Install]
WantedBy=multi-user.target
Manage it with:
rpi ~$ sudo systemctl enable --now myprogram.service
rpi ~$ sudo systemctl status myprogram.service
rpi ~$ sudo systemctl stop myprogram.service
rpi ~$ sudo systemctl start myprogram.service
Answered by Ingo on December 4, 2021
I think what's happening is the module is present, but not where it's being looked for. 2 things, running at boot it may be running as root. This changes paths among other things, also, there's when it's started relative to other things being set up. So, first, try using the full path name ( import /... ??/wiringpi ) ( wherever it is ). If that doesn't work, delay the start. The "correct" way to start jobs on boot ( I don't know it well ) does have a way to say either "requires" or "wait for".
Answered by daveofeves on December 4, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP