无法让服务在 Rock64 SBC 上运行

无法让服务在 Rock64 SBC 上运行

我有一个 python 脚本,可以在连接到我的设备 (Rock64 SBC) 的 LCD 屏幕上显示当前的 IP 地址和时间。

python 脚本本身运行良好。

from subprocess import check_output
from time import sleep
from datetime import datetime
from RPLCD.i2c import CharLCD
lcd = CharLCD('PCF8574', 0x27, auto_linebreaks=False)
lcd.clear()
def get_ip():
    cmd = "hostname -I | cut -d\' \' -f1"
    return check_output(cmd, shell=True).decode("utf-8").strip()
while True:
    lcd_line_1 = datetime.now().strftime('%b %d  %H:%M:%S')
    lcd_line_2 = "IP " + get_ip()

    lcd.home()
    lcd.write_string(f'{lcd_line_1}\r\n{lcd_line_2}')
    sleep(10)

那么lcd.service的服务代码是这样的:

[Unit]
Description=LCD IP Display
After=multi-user.target

[Service]
Type=simple
ExecStart=/bin/sh -c "python3 /home/rock64/pi/lcd_ip.py"
WorkingDirectory=/home/rock64/pi
Restart=always
User=rock64

[Install]
WantedBy=multi-user.target

systemctl 状态 LCD.service显示:

rock64@rock64:/etc/systemd/system$ systemctl status lcd.service
● lcd.service - LCD IP Display
   Loaded: loaded (/etc/systemd/system/lcd.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Thu 2021-08-19 15:10:11 CDT; 11s ago
  Process: 5029 ExecStart=/bin/sh -c python3 /home/rock64/pi/lcd_ip.py (code=exited, status=1/FAILURE)
 Main PID: 5029 (code=exited, status=1/FAILURE)

Aug 19 15:10:11 rock64 systemd[1]: lcd.service: Service hold-off time over, scheduling restart.
Aug 19 15:10:11 rock64 systemd[1]: lcd.service: Scheduled restart job, restart counter is at 15.
Aug 19 15:10:11 rock64 systemd[1]: Stopped LCD IP Display.
Aug 19 15:10:11 rock64 systemd[1]: lcd.service: Start request repeated too quickly.
Aug 19 15:10:11 rock64 systemd[1]: lcd.service: Failed with result 'exit-code'.
Aug 19 15:10:11 rock64 systemd[1]: Failed to start LCD IP Display.
rock64@rock64:/etc/systemd/system$ journalctl _PID=5029
-- Logs begin at Sun 2018-01-28 09:58:18 CST, end at Thu 2021-10-14 12:18:00 CDT. --
Aug 19 15:10:10 rock64 sh[5029]: Traceback (most recent call last):
Aug 19 15:10:10 rock64 sh[5029]:   File "/home/rock64/pi/lcd_ip.py", line 5, in <module>
Aug 19 15:10:10 rock64 sh[5029]:     lcd = CharLCD('PCF8574', 0x27, auto_linebreaks=False)
Aug 19 15:10:10 rock64 sh[5029]:   File "/home/rock64/.local/lib/python3.6/site-packages/RPLCD/i2c.py", line 168, in __i
Aug 19 15:10:10 rock64 sh[5029]:     auto_linebreaks=auto_linebreaks)
Aug 19 15:10:10 rock64 sh[5029]:   File "/home/rock64/.local/lib/python3.6/site-packages/RPLCD/lcd.py", line 98, in __in
Aug 19 15:10:10 rock64 sh[5029]:     self._init_connection()
Aug 19 15:10:10 rock64 sh[5029]:   File "/home/rock64/.local/lib/python3.6/site-packages/RPLCD/i2c.py", line 173, in _in
Aug 19 15:10:10 rock64 sh[5029]:     self.bus = SMBus(self._port)
Aug 19 15:10:10 rock64 sh[5029]: PermissionError: [Errno 13] Permission denied

我有 chmod +x 主 python 文件以及文件 i2c.py 和 lcd.py。

ls -ls 显示

rock64@rock64:~/pi$ ls -la lcd_ip.py
-rwxr-xr-x 1 rock64 sudo 500 Aug 13 17:08 lcd_ip.py

最后,我将以下几行添加到 visudo

rock64 ALL=(ALL) NOPASSWD: /etc/systemd/system/lcd.service
rock64 ALL=(ALL) NOPASSWD: /home/rock64/pi/lcd_ip.py
rock64 ALL=(ALL) NOPASSWD: /home/rock64/.local/lib/python3.6/site-packages/RPLCD/ic2.py
rock64 ALL=(ALL) NOPASSWD: /home/rock64/.local/lib/python3.6/site-packages/RPLCD/lcd.py

我仍然收到“错误 13 权限被拒绝”。

有什么建议么?

答案1

我必须做两件事:在服务文件中,将 ExecStart 更改为

ExecStart=/usr/bin/python3 /home/rock64/pi/lcd_ip.py

然后我跑去sudo visudo添加

rock64 ALL=(ALL) NOPASSWD: ALL

相关内容