rc local on raspberry pi4 unable to run the script while bootup

174 Views Asked by At

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

1

There are 1 best solutions below

2
ukBaz On

You might want to start your script using systemd rather than rc.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.local

For your example I think it would be something like the following five steps

  1. Create a /etc/systemd/system/weighingmachine.service file, containing:
[Unit]
Description=weighingmachine

[Service]
Type=exec
ExecStart=/home/cf2023/Desktop/weighingmachine/venv/bin/python3 /home/cf2023/Desktop/weighingmachine/execute123.py

[Install]
WantedBy=multi-user.target
  1. Make it run at boot with sudo systemctl enable weighingmachine.service

  2. Start it with sudo systemctl start weighingmachine.service

  3. Check the status with sudo systemctl status weighingmachine.service

  4. View the output with sudo journalctl --unit=weighingmachine.service

You seem to have problems starting the service so I'm including a transcript of a test I've done.


$ cat /etc/systemd/system/weighingmachine.service
[Unit]
Description=weighingmachine

[Service]
Type=exec
ExecStart=/home/pi/runner.sh

[Install]
WantedBy=multi-user.target
$ cat /home/pi/runner.sh 
#! /bin/sh
echo "Hello, World"
$ /home/pi/runner.sh
Hello, World
$ sudo systemctl start weighingmachine.service 
$ sudo journalctl --unit=weighingmachine.service
-- Journal begins at Mon 2022-04-04 13:17:02 BST, ends at Fri 2023-12-22>
Dec 22 08:34:05 SensePi systemd[1]: Starting weighingmachine...
Dec 22 08:34:05 SensePi systemd[1]: Started weighingmachine.
Dec 22 08:34:05 SensePi runner.sh[31535]: Hello, World
Dec 22 08:34:05 SensePi systemd[1]: weighingmachine.service: Succeeded.
$ sudo systemctl enable weighingmachine.service 
Created symlink /etc/systemd/system/multi-user.target.wants/weighingmachine.service → /etc/systemd/system/weighingmachine.service.
$ sudo systemctl status weighingmachine.service 
● weighingmachine.service - weighingmachine
     Loaded: loaded (/etc/systemd/system/weighingmachine.service; enable>
     Active: inactive (dead)

Dec 22 08:34:05 SensePi systemd[1]: Starting weighingmachine...
Dec 22 08:34:05 SensePi systemd[1]: Started weighingmachine.
Dec 22 08:34:05 SensePi runner.sh[31535]: Hello, World
Dec 22 08:34:05 SensePi systemd[1]: weighingmachine.service: Succeeded.

The error you are getting is often caused if you put spaces around the equal sign (=) in the service file.