I want to run a script.sh file using rclocal in raspberry pi4b the script details is given below
#! /bin/bash
cd /home/cf2023/Desktop/weighingmachine
source venv/bin/activate
python3 execute123.py
I am using following code in bash
sudo nano/etc/rc.local
then in rc.local I am adding following script before exit 0
/home/desktop/run_script.sh
then i am running following code in bash
chmod +x /home/desktop/run_script.sh
after that i rebooted, but the code did not run on startup
then i tried in bash
systemctrl start rc.local.service
but it requires an authentication, which i provided successfully, but again it gives nothing just after providing authentication it remains like that
I was expecting that the run_script.sh should run while rebooting.
please help me out
Edit 1:
ALSA: could not open audio device: device or resource busy.
same program is running in bash very well but while i tried to run it using systemd it is giving above error
Edit 2
weighingmachine.service - Weighing Machine Service
Loaded: loaded (/etc/systemd/system/weighingmachine.service; enabled; preset: enabled)
Active: failed (Result: exit-code) since Fri 2023-12-22 16:09:28 IST; 2s ago
Duration: 923ms
Process: 2630 ExecStart=/home/cf2023/Desktop/weighingmachine/venv/bin/python3 /home/cf2023/Desktop/weighingmachine/1912231341.py (code=exited, status=1/FAILURE)
Main PID: 2630 (code=exited, status=1/FAILURE)
CPU: 816ms
Dec 22 16:09:28 raspberrypi systemd[1]: Started weighingmachine.service - Weighing Machine Service.
Dec 22 16:09:28 raspberrypi python3[2630]: pygame 2.5.2 (SDL 2.28.3, Python 3.11.2)
Dec 22 16:09:28 raspberrypi python3[2630]: Hello from the pygame community. https://www.pygame.org/contribute.html
Dec 22 16:09:28 raspberrypi python3[2630]: [Error initializing pygame mixer]: ALSA: Couldn't open audio device: Device or resource busy
Dec 22 16:09:28 raspberrypi systemd[1]: weighingmachine.service: Main process exited, code=exited, status=1/FAILURE
Dec 22 16:09:28 raspberrypi systemd[1]: weighingmachine.service: Failed with result 'exit-code'.
Edit 3
What is wrong with following service file, it is not giving the desired output
[Unit]
Description=Weighing Machine Service
After=multi-user.target
[Service]
Type=simple
Environment="DISPLAY=:0"
Environment="/home/cf2023/Desktop/weighingmachine/venv"
ExecStart=/home/cf2023/Desktop/weighingmachine/venv/bin/python3 /home/cf2023/De>
Restart=always
RestartSec= 5
KillMode=process
SendSIGHUP=no
[Install]
WantedBy=graphical.target
Edit 4
i am running the Python script using systemd, the Python program is suppose to give output as sound through the audio channel of Raspberry Pi, but it is not giving that. Rather the output is coming through the tv monitor which I am using for Raspberry Pi . Same program is giving correct sound output normally, but while using systemd the problem is coming.
service file is
!/bin/bash
[Unit]
Description=Weighing Machine Service
After=multi-user.target
[Service]
Type=simple
User=cf2023
Environment="DISPLAY=:0"
Environment="XAUTHORITY=home/pi/.Xauthority"
WorkingDirectory=/home/cf2023/Desktop/weighingmachine
ExecStart=/home/cf2023/Desktop/weighingmachine/venv/bin/python3 /home/cf2023/De>
Restart=always
RestartSec=5
KillMode=process
SendSIGHUP=no
[Install]
WantedBy=graphical.target
You might want to start your script using
systemdrather thanrc.local, if for no other reason than you can get debug messages out to help figure our what is going wrong. More information at https://blog.usedbytes.com/2019/11/run-at-startup-without-rc.localFor your example I think it would be something like the following five steps
/etc/systemd/system/weighingmachine.servicefile, containing:Make it run at boot with
sudo systemctl enable weighingmachine.serviceStart it with
sudo systemctl start weighingmachine.serviceCheck the status with
sudo systemctl status weighingmachine.serviceView the output with
sudo journalctl --unit=weighingmachine.serviceYou seem to have problems starting the service so I'm including a transcript of a test I've done.
The error you are getting is often caused if you put spaces around the equal sign (
=) in the service file.