如何找到 systemd 服务无法启动的确切原因(服务的单元文件设置错误)?

Geo*_*e Y 2 systemd systemd-service

这真的很烦人,systemd只回应我的服务文件配置错误,但没有具体指出错误在哪里:

\n

/lib/systemd/system/auto_pgha.service:

\n
[Unit]\nDescription=PostgreSQL High Availability\nAfter=network.service\nAfter=firewalld.service\n\n\n[Service]\nType=simple\nWorkingDirectory=/etc/repmgr\nExecStartPre=/bin/bash -c 'echo -e "\\n"  `date +"%Y/%m/%d %a, %X"`": STARTING \\n"  >> pgha.log'\nExecStart=/bin/bash -c "python3 pg_high_availability.py  &>> pgha.log"\nRestart=always\nRestartSec=3\n\n[Install]\nWantedBy=multi-user.target\n
Run Code Online (Sandbox Code Playgroud)\n

在目录内/etc/repmgr这两个命令运行得很好。但 systemd 服务只是响应了一个错误:

\n
# systemctl start auto_pgha\nFailed to start auto_pgha.service: Unit auto_pgha.service has a bad unit file setting.\nSee system logs and 'systemctl status auto_pgha.service' for details.\n\n# systemctl status -l auto_pgha\n\xe2\x97\x8b auto_pgha.service - PostgreSQL High Availability\n     Loaded: bad-setting (Reason: Unit auto_pgha.service has a bad unit file setting.)\n......\nauto_pgha.service: Unit configuration has fatal error, unit will not be be started. \n
Run Code Online (Sandbox Code Playgroud)\n

lar*_*sks 8

您可以使用该systemd-analyze verify命令。如果我们将您问题的内容放入 中pgha.service,我们会看到:

$ systemd-analyze verify pgha.service
.../pgha.service:10: Failed to resolve unit specifiers in echo -e "
"  `date +"%Y/%m/%d %a, %X"`": STARTING
"  >> pgha.log: Invalid slot
pgha.service: Unit configuration has fatal error, unit will not be started.
Unit pgha.service has a bad unit file setting.
Run Code Online (Sandbox Code Playgroud)

您收到此错误是因为 systemd 本身使用%<something>令牌(请参阅手册页的“说明符”部分)systemd.unit(5)

你需要写:

ExecStartPre=/bin/bash -c 'echo -e "\n"  `date +"%%Y/%%m/%%d %%a, %%X"`": STARTING \n"  >> pgha.log'
Run Code Online (Sandbox Code Playgroud)