You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派4B rc.local/systemd启动脚本失败及音频输出异常求助

树莓派4B开机自启脚本及音频输出问题

一、rc.local尝试失败

我在树莓派4B上尝试通过rc.local运行script.sh脚本,脚本内容如下:

#! /bin/bash
cd /home/cf2023/Desktop/weighingmachine
source venv/bin/activate
python3 execute123.py

执行sudo nano /etc/rc.local,在exit 0前添加/home/desktop/run_script.sh,并执行chmod +x /home/desktop/run_script.sh,但重启后脚本未运行;尝试systemctl start rc.local.service无响应。

二、systemd服务问题

改用systemd创建weighingmachine.service后,出现ALSA: could not open audio device: device or resource busy错误,服务启动失败,状态信息如下:

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'.

调整服务文件后,服务仍无法正常工作,Python脚本的音频输出从连接的TV显示器发出,而非树莓派音频通道,手动正常运行脚本时音频输出正常。调整后的服务文件内容如下:

[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/Desktop/weighingmachine/1912231341.py
Restart=always
RestartSec=5
KillMode=process
SendSIGHUP=no

[Install]
WantedBy=graphical.target

内容的提问来源于stack exchange,提问作者paritoshnitc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 21:35:59