Python 프로그램을 서비스로 사용할 수 없습니다.

Python 프로그램을 서비스로 사용할 수 없습니다.

시작 시 매우 간단한 Python 프로그램을 실행하고 자체적으로 다시 시작하려고 합니다.

Python 프로그램은 터미널에서 제대로 실행됩니다. 서비스에서 실패 메시지만 표시합니다.

sudo systemctl status logger_autostart.service
logger_autostart.service - Logging Signal Strength of Rockblock Iridium
     Loaded: loaded (/etc/systemd/system/logger_autostart.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Thu 2023-12-07 07:55:57 CET; 1s ago
    Process: 3340 ExecStart=/usr/bin/python3 /home/pi/Downloads/signal_test.py (code=exited, status=1/FAILURE)
   Main PID: 3340 (code=exited, status=1/FAILURE)
        CPU: 204ms

Dec 07 07:55:57 raspberrypi systemd[1]: logger_autostart.service: Scheduled restart job, restart counter is at 5.
Dec 07 07:55:57 raspberrypi systemd[1]: Stopped Logging Signal Strength of Rockblock Iridium.
Dec 07 07:55:57 raspberrypi systemd[1]: logger_autostart.service: Start request repeated too quickly.
Dec 07 07:55:57 raspberrypi systemd[1]: logger_autostart.service: Failed with result 'exit-code'.
Dec 07 07:55:57 raspberrypi systemd[1]: Failed to start Logging Signal Strength of Rockblock Iridium.

내 서비스 파일:

[Unit]
Description=Logging Signal Strength of Rockblock Iridium
After=multi-user.target

[Service]
Type=simple
User=pi
ExecStart=/usr/bin/python3 /home/pi/Downloads/signal_test.py
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=%n

[Install]
WantedBy=multi-user.target

다음을 사용하면 오류 로그가 표시되지 않습니다. journalctl -f -u echo-logger_autostart.service

프로그램:

import time
from datetime import datetime
import RPi.GPIO as GPIO
import time
import serial
from adafruit_rockblock import RockBlock
import logging
import random
import os
from threading import Event

# GPIO pins for LEDs
led_pins = [17, #green (no connection)
            27, #yellow (bad connection)
            22, #red (good connection)
            23] #blue (during startup / for )
# GPIO pin for the button
button_pin = 18

loop_active = False

def Board_Setup():
    # Set GPIO mode and warnings
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)

    # Setup LED pins as output
    for pin in led_pins:
        GPIO.setup(pin, GPIO.OUT)
        GPIO.output(pin, GPIO.LOW)

    # Setup button pin as input with pull-up resistor
    GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def rockblock_setup() -> RockBlock:
    uart = serial.Serial("/dev/ttyUSB0", 19200)
    return RockBlock(uart)

def all_leds_off():
    for pin in led_pins:
        GPIO.output(pin, GPIO.LOW)

def red_led_on():
    GPIO.output(led_pins[2], GPIO.HIGH)

def yellow_led_on():
    GPIO.output(led_pins[1], GPIO.HIGH)

def green_led_on():
    GPIO.output(led_pins[0], GPIO.HIGH)

def toggle_blue_led():
    state = GPIO.input(led_pins[3])
    if state == GPIO.HIGH:
        GPIO.output(led_pins[3], GPIO.LOW)
    else:
        GPIO.output(led_pins[3], GPIO.HIGH)

def button_pressed_callback(channel):

    global loop_active

    loop_active = not loop_active

def create_log_file():
    # Create logfile using current time in Y-M-D
    if not os.path.exists("logs/"):
        os.makedirs("logs/")

    current_datetime = datetime.now()
    timestamp = current_datetime.strftime("%Y-%m-%d")
    log_file_name = f"log_{timestamp}.txt"
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s %(message)s",
        datefmt="%H:%M:%S",
        filename = "logs/" + log_file_name
    )

def run_logger():
    # log signal strength every 10s
    global loop_active
    create_log_file()
    rb = rockblock_setup()
    # wait until button is pressed
    GPIO.add_event_detect(button_pin, GPIO.BOTH, callback=button_pressed_callback)
    print("use switch to start logging")
    while not loop_active:
        pass
    print("logging started")
    while loop_active:

        
        quality = rb.signal_quality
        # random int while antenna gets fixed
        # quality = random.randint(0,5)
        logging.info(quality)
        print(quality)
        if quality == 0:
            all_leds_off()
            red_led_on()
        elif quality <= 3:
            all_leds_off()
            yellow_led_on()
        elif quality <= 5:
            all_leds_off()
            green_led_on()

        time.sleep(3)

def main():
    Board_Setup()
    # Run Logger after button press
    toggle_blue_led()

    try:        
        run_logger()
    finally:
        all_leds_off()
        GPIO.cleanup

if __name__ == '__main__':
    main()

내가 찾을 수 있는 유일한 "흥미로운" 동작은 sudo를 사용할 때 Python 프로그램이 작동하지 않지만 sudo -E를 사용할 때는 작동한다는 것입니다.

나는 Python이나 우분투 서비스에 익숙하지 않기 때문에 현재 이것을 디버깅하는 데 정말 어려움을 겪고 있습니다.

서비스에서 .sh 프로그램으로 시작하려고 시도했지만 서비스에 User=pi를 추가했지만 성공하지 못했습니다.

관련 정보