pygame 脚本从命令行运行良好,但在服务中失败

pygame script runs fine from command line, but fails in service

过去很多天一直面临一个问题 - 在互联网上搜索了很多,并尝试了各种选择,但 none 奏效了,因此 post。

简而言之,我有一个 python 脚本,它在 raspberry pi 零的命令行上工作得很好,但是,当执行相同的服务时,它会不断被杀死或终止。无法确定原因

Apr  4 16:38:16 zeropi systemd[1]: pyGameDisplay.service: Enqueued job pyGameDisplay.service/restart as 4267
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitNew cookie=1 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitNew cookie=1619 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=JobNew cookie=2 reply_cookie=0 signature=uos error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=JobNew cookie=1620 reply_cookie=0 signature=uos error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=method_return sender=org.freedesktop.systemd1 destination=n/a path=n/a interface=n/a member=n/a cookie=3 reply_cookie=1 signature=o error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: home.mount: Collecting.
Apr  4 16:38:16 zeropi systemd[1]: home-sundeep.mount: Collecting.
Apr  4 16:38:16 zeropi systemd[1]: home-sundeep-display.mount: Collecting.
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitNew cookie=4 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitNew cookie=1621 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitRemoved cookie=5 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitRemoved cookie=1622 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitNew cookie=6 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitNew cookie=1623 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitRemoved cookie=7 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitRemoved cookie=1624 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitNew cookie=8 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitNew cookie=1625 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitRemoved cookie=9 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitRemoved cookie=1626 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitNew cookie=10 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=UnitNew cookie=1627 reply_cookie=0 signature=so error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Got message type=method_call sender=n/a destination=org.freedesktop.systemd1 path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=GetUnit cookie=2 reply_cookie=0 signature=s error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=method_return sender=org.freedesktop.systemd1 destination=n/a path=n/a interface=n/a member=n/a cookie=11 reply_cookie=2 signature=o error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: pyGameDisplay.service: Job 4267 pyGameDisplay.service/restart finished, result=done
Apr  4 16:38:16 zeropi systemd[1]: pyGameDisplay.service: Converting job pyGameDisplay.service/restart -> pyGameDisplay.service/start
Apr  4 16:38:16 zeropi systemd[1]: pyGameDisplay.service: Passing 0 fds to service
Apr  4 16:38:16 zeropi systemd[1]: pyGameDisplay.service: About to execute: /usr/bin/python3 /home/sundeep/display/pyGameDisplay.py
Apr  4 16:38:16 zeropi systemd[1]: pyGameDisplay.service: Forked /usr/bin/python3 as 7477
Apr  4 16:38:16 zeropi systemd[7477]: pyGameDisplay.service: Executing: /usr/bin/python3 /home/sundeep/display/pyGameDisplay.py
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=12 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=13 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=1628 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=1629 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: pyGameDisplay.service: Changed dead -> running
Apr  4 16:38:16 zeropi systemd[1]: pyGameDisplay.service: Job 4267 pyGameDisplay.service/start finished, result=done
Apr  4 16:38:16 zeropi systemd[1]: Started pyGameDisplay.
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=14 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=15 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=1630 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=1631 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=JobRemoved cookie=16 reply_cookie=0 signature=uoss error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1 interface=org.freedesktop.systemd1.Manager member=JobRemoved cookie=1632 reply_cookie=0 signature=uoss error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=17 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=18 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=1633 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=1634 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: pyGameDisplay.service: User lookup succeeded: uid=0 gid=0
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=19 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=org.freedesktop.systemd1 destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=20 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=1635 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=1636 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Got message type=method_call sender=n/a destination=org.freedesktop.systemd1 path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=Get cookie=3 reply_cookie=0 signature=ss error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Sent message type=method_return sender=org.freedesktop.systemd1 destination=n/a path=n/a interface=n/a member=n/a cookie=21 reply_cookie=3 signature=v error-name=n/a error-message=n/a
Apr  4 16:38:16 zeropi systemd[1]: Bus private-bus-connection: changing state RUNNING → CLOSING
Apr  4 16:38:16 zeropi systemd[1]: Bus private-bus-connection: changing state CLOSING → CLOSED
Apr  4 16:38:16 zeropi systemd[1]: Got disconnect on private connection.
Apr  4 16:38:26 zeropi systemd[1]: Received SIGCHLD from PID 7477 (python3).
Apr  4 16:38:26 zeropi systemd[1]: Child 7477 (python3) died (code=killed, status=1/HUP)
Apr  4 16:38:26 zeropi systemd[1]: pyGameDisplay.service: Child 7477 belongs to pyGameDisplay.service.
Apr  4 16:38:26 zeropi systemd[1]: pyGameDisplay.service: Main process exited, code=killed, status=1/HUP
Apr  4 16:38:26 zeropi systemd[1]: pyGameDisplay.service: Succeeded.
Apr  4 16:38:26 zeropi systemd[1]: pyGameDisplay.service: Changed running -> dead
Apr  4 16:38:26 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=1637 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:26 zeropi systemd[1]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/systemd1/unit/pyGameDisplay_2eservice interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=1638 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Apr  4 16:38:26 zeropi systemd[1]: pyGameDisplay.service: Changed dead -> auto-restart

单元文件是


[Unit]
Description = pyGameDisplay
After = multi-user.target syslog.target network.target network-online.target graphical.target

[Service]
Type=simple
Environment="DISPLAY=:0"
#Environment="XAUTHORITY=/home/pi/.Xauthority"
#Type=notify
Restart=always
#Restart= on-failure
WorkingDirectory=/home/sundeep/display/
ExecStart =/usr/bin/python3 /home/sundeep/display/pyGameDisplay.py
RestartSec = 90
TimeoutStartSec = 69
StandardOutput=append:/var/log/pyDisplay/stdout.log
StandardError=append:/var/log/pyDisplay/stderror.log
SyslogIdentifier=pyGameDisplay
SendSIGHUP=no
KillMode = process
User=root

[Install]
WantedBy = multi-user.target

请告知如何进一步调试。

我想它终于解决了。

在脚本中向上移动了以下代码

def handler(signum, frame):
    """Why is systemd sending sighups? I DON'T KNOW."""
    # logging.debug("Got a {} signal.Exiting !!!!".format(signum))

    if signum == signal.SIGTERM:
        logging.debug("Got a {} signal.Exiting !!!!".format(signum))
        pygame.display.quit()
        pygame.quit()
        exit()
    else:
        logging.debug("Got a {} signal. Doing nothing".format(signum))

更重要的是,在较早的代码中进一步移动。

        signal.signal(signal.SIGHUP, handler)
        signal.signal(signal.SIGTERM, handler)
        signal.signal(signal.SIGCONT, handler)

我认为因为这个信号被及时困住,阻止退出

08:50:36,199 root DEBUG Got a 1 signal. Doing nothing
08:50:36,201 root DEBUG Got a 18 signal. Doing nothing

虽然 - 我第一次启动服务时确实得到关注​​

08:50:09,959 root DEBUG Got a 15 signal.Exiting !!!!```