0

I have a python script that displays the current IP address and time to a LCD screen attached to my device (Rock64 SBC).

The python script runs fine on it's own.

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)

Then the service code for lcd.service is this:

[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 status lcd.service displays:

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

I have chmod +x the main python file and the files i2c.py and lcd.py.

ls -ls shows

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

lastly, I have added the following lines to 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

and I still get "error 13 permission denied".

Any suggestions?

Shawn
  • 105
  • 3
  • I changed ExecStart to `ExecStart=/usr/bin/python3 /home/rock64/pi/lcd_ip.py` but still get permission denied – Shawn Aug 20 '21 at 15:57

1 Answers1

0

I had to do 2 things: In the service file, change ExecStart to

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

Then I ran sudo visudo and added

rock64 ALL=(ALL) NOPASSWD: ALL
Shawn
  • 105
  • 3