For context, I am using Raspberry Pi model 3B+. Currently I am trying to run a python script at the Pi's boot up. The script uses the module face_recognition, and everything works fine when running it normally or through the terminal.
But as soon as I try running it automatically when the pi boots up I get the following error:
Traceback (most recent call last):
File "/home/pi/Desktop/code/please_work_2.py", line 6, in <module>
import face_recognition
ImportError: No module named face_recognition
I googled a bit and I think it has to do something with not setting the environment in the service file correctly. It is a bit messy as of right now, but I am new to working with these kind of files so I am struggling with finding out how to get it to work. My service file right now:
[Unit]
Description=Start Bling
[Service]
Environment=DISPLAY=:0
WorkingDirectory=/home/pi/facial_recognition
Environment=XAUTHORITY=/home/pi/.Xauthority
Environment="prog_path"=/home/pi/facial_recognition
ExecStart=/usr/bin/python /home/pi/facial_recognition/run_on_start.py
Restart=always
RestartSec=10s
KillMode=process
TimeoutSec=infinity
[Install]
WantedBy=graphical.target
The program does not necessarily need to run in Desktop auto-login so if there is a possible fix in the console version that is fine as well. I just have it this way currently, so it is easier to check if the program is working as intended.
EDIT:
I have also tried using crontab, but then nothing happened on reboot. Not with and not without the '&' at the end.
As of right now, as was suggested below is that when I add
User = pi
the program does launch, but I am struggling with getting it to launch once instead every 10s. Deleting that bit does not help since then it stops launching at all.
Can you try this, add the following User=pi under [Service] and see if it works.
I think the way you did it, you are trying to launch as another user or sudo user, you have your stuff only installed in your current pi user path, so try launching the script as the user pi.
Your service file should look like this.
[Unit]
Description=Start Bling
[Service]
User=pi
Environment=DISPLAY=:0
WorkingDirectory=/home/pi/facial_recognition
Environment=XAUTHORITY=/home/pi/.Xauthority
Environment="prog_path"=/home/pi/facial_recognition
ExecStart=/usr/bin/python /home/pi/facial_recognition/run_on_start.py
Restart=always
RestartSec=10s
KillMode=process
TimeoutSec=infinity
[Install]
WantedBy=graphical.target
Related
I have my_robot_ros.service file which auto run a launch during boot.
[Unit]
Description="bringup my_robot_ros"
After=network.target
[Service]
Type=simple
ExecStart=/usr/sbin/my_robot_ros-start
[Install]
WantedBy=multi-user.target
My launch file works just fine when I run it in the terminal but when it runs with my_robot_ros.service it has errors regarding permission in the folder as shown below.
clcik image
I think it is the reason why my imageprocessing node dies or stop working. Does anyone know how to solve this problem? Thank you
The "correct" solution here is to structure your code so it doesn't use pre-created files in /tmp. By default only the file creator can modify files directly in /tmp. And your error happens because running a service file will run commands under a different user. The quick workaround for this would be to remove the sticky bit in the folder via: sudo chmod -t /tmp.
Note that I wouldn't really recommend this in general, though.
i have a file for my discord bot which i run on boot and now i also made a react.js app and i also want this to run on boot. But when i put:
cd /home/pi/kickzraptor-site && npm run start
also in my .bashrc file, only the first one gets run because its an infinite loop i think. How can i run both on startup? Thanks! (This is the line already at the bottom of my bashrc file)
echo Running bot.py scipt...
sudo python3 /home/pi/bot.py
Fastest way (not recommended) is to add & at the end of the command so that the program doesn't block for further processes. sudo python3 /home/pi/bot.py &
Recommended way is to create a systemd service that runs during or after the boot is completed (depending on its configuration). This method is also good for error handling and it provides more ability on the program.
I am a pretty new python programmer and am a little bit familiar with crontab. What I am trying to do is probably not the best practice but it is what I am most familiar with.
I have a raspberry pi with a couple python scripts I want to run on boot and stay running in the background. They are infinite loop programs. They are tested and working in a cmd terminal and have been function for a couple weeks. Just getting tired on manually starting them up. When the pi goes through a power cycle.
So I did a sudo crontab -e and added this line as my only entry
#reboot /usr/bin/python3 /usr/bin/script.py &
If I copy paste this exactly (minus the #reboot) it will run successfully in the cmd line.
I am using a cmd:
pgrep -af pythonto check to see if it is running. I normally see two scripts running there but not the one I am trying to add.
I am not sure where I am going wrong or my best method to troubleshoot my issue. From the research I have been doing it seems like it should work.
Thanks for your help
Kevin
You might find it easier to create a systemd service file for each program that you want to start when you Raspberry Pi boots. systemd comes with a few more tools to help you debug your configuration.
This is what an example systemd service file (located at /etc/systemd/system/myscript.service) would look like:
[Unit]
Description=My service
After=network.target
[Service]
ExecStart=/usr/bin/python3 /usr/bin/script.py
WorkingDirectory=/home/pi/myscript
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
and then you can enable this program to run on boot with the command:
sudo systemctl enable myscript.service
These examples are from Raspberry Pi's documentation about systemd. But because systemd is widely used in the Linux world, so you can also follow documentation and StackOverflow answers for other Linux distributions.
I have a Python application myapp on a Raspberry Pi, with a front end control panel made with Dash. I run the Dash app in its own thread so that I can use it to manipulate some settings inside myapp.
When I SSH in to the Raspberry Pi, I want to start the Python application myapp in the background and then close the remote shell window and just let it spin and do its thing. After having written this question I found I have to use nohup for this according to
nohup python path/to/myapp.py &
For other python apps just
python path/to/other_app &
seems to suffice.
So I guess I already have the answer to my question. But, while at the subject, is this the preferred and only solution?
It depends a lot on what specific linux variant you're running, but generally speaking the best way is to your let your system's service manager handle that. In most cases these days that means systemd.
Create a service config file-
[Unit]
Description=My Python Service
[Service]
Type=simple
ExecStart=/path/to/my/python/service.py
[Install]
WantedBy=multi-user.target
Put this in the /lib/systemd/system/ with a name like mypythonapp.service.
Run systemctl daemon-reload so systemd knows to look for the new file.
Run systemctl enable mypythonapp.service to tell it to run the app on start.
Run systemctl start mypythonapp.service to tell it to run the app immediately.
Now your script will have logging, will restart when crashed or when the system reboots, and you don't have to manually kick it off.
Note: I'm new to everything so bare with me.
I'm using a RPi 4B with Buster. My goal is to automatically run 2 python scripts at the same time when the pi first boots up. Both scripts are in a virtual environment. The first script is called sensor.py which basically uses an ultrasonic distance sensor to continuously calculate distances between the sensor and an object. The other is an object recognition script from Tensorflow Lite called TFLite_detection_webcam.py that identifies objects from a camera feed. I can't use rc.local for autorunning because the object recognition script uses a picamera feed as an input, which rc.local doesn't support. So my preferred option is using autostart. I was able to successfully get the sensor.py script to autorun by issuing this in the terminal: sudo nano /etc/xdg/lxsession/LXDE-pi/autostart and adding this to it: /home/pi/tflite1/tflite1-env/bin/python3 /home/pi/tflite1/sensor.py. In this case, tflite1-env is the virtual environment being activated. However, I don't know how to get the second script to run. To run it regularly, I would issue the following into the terminal and the camera feed would pop up on the screen as a window.
cd tflite1
source tflite1-env/bin/activate
python3 TFLite_detection_webcam.py --modeldir=TFLite_model
I've tried to get this script to run by adding this to the autostart file: /home/pi/tflite1/tflite1-env/bin/python3 /home/pi/tflite1/TFLite_detection_webcam.py --modeldir=TFLite_model but it doesn't seem to be working. I've tried to run it using shell files, but every time that I run a shell file in the autostart file such as adding ./launch.sh to the bottom, nothing happens. Any help getting the second script to run at the same time as the first upon startup would be greatly appreciated. Thanks in advance.
Use Systemd. Set up Systemd unit files in /etc/systemd/system, e.g.
kitkats-sensor.unit
[Unit]
After=network.target
[Service]
ExecStart=/home/pi/tflite1/tflite1-env/bin/python3 /home/pi/tflite1/sensor.py
WorkingDirectory=/home/pi/tflite1/
User=pi
Group=pi
kitkats-tflite.unit
[Unit]
After=network.target
[Service]
ExecStart=/home/pi/tflite1/tflite1-env/bin/python3 /home/pi/tflite1/TFLite_detection_webcam.py --modeldir=TFLite_model
WorkingDirectory=/home/pi/tflite1/
User=pi
Group=pi
Then enable the unit files with systemctl enable kitkats-tflite and systemctl enable kitkats-sensor (to have them autostart) and systemctl start kitkats-tflite (and sensor) to start them right away.
You can then see them in e.g. systemctl, and their logs are diverted to journalctl.