Python 脚本的 systemd 服务激活失败

问题描述

我想注册一个python脚本作为守护程序服务,在系统启动时执行并在后台持续运行。该脚本打开网络套接字、本地日志文件并执行多个线程。该脚本格式良好,运行时没有任何编译或运行时问题。

我使用以下服务文件进行注册

[Unit]
Description=ModBus2knx Gateway Daemon
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/bin/ModBusDaemon.py

[Install]
WantedBy=multi-user.target

启动服务导致以下错误

● ModBusDaemon.service - ModBus2knx Gateway Daemon
   Loaded: loaded (/lib/systemd/system/ModBusDaemon.service; enabled; vendor preset: enabled)
   Active: Failed (Result: exit-code) since Mon 2021-01-04 21:46:29 CET; 6min ago
  Process: 1390 ExecStart=/usr/bin/python3 /usr/bin/ModBusDaemon.py (code=exited,status=1/FAILURE)
 Main PID: 1390 (code=exited,status=1/FAILURE)

Jan 04 21:46:29 raspBerrypi systemd[1]: Started ModBus2knx Gateway Daemon.
Jan 04 21:46:29 raspBerrypi systemd[1]: ModBusDaemon.service: Main process exited,code=exited,status=1/FAILURE
Jan 04 21:46:29 raspBerrypi systemd[1]: ModBusDaemon.service: Failed with result 'exit-code'.

感谢您的支持

解决方法

相关帖子让我找到了解决我的问题的方法。 Ubuntu systemd custom service failing with python script 指的是同样的问题。将 WorkingDirectory 添加到 Service 部分的建议解决方案为我解决了这个问题。不过,我找不到足够的 systemd 文档概述隐式依赖。