![如何找到 systemd 服务无法启动(服务的单元文件设置不正确)的确切原因?](https://linux22.com/image/789178/%E5%A6%82%E4%BD%95%E6%89%BE%E5%88%B0%20systemd%20%E6%9C%8D%E5%8A%A1%E6%97%A0%E6%B3%95%E5%90%AF%E5%8A%A8%EF%BC%88%E6%9C%8D%E5%8A%A1%E7%9A%84%E5%8D%95%E5%85%83%E6%96%87%E4%BB%B6%E8%AE%BE%E7%BD%AE%E4%B8%8D%E6%AD%A3%E7%A1%AE%EF%BC%89%E7%9A%84%E7%A1%AE%E5%88%87%E5%8E%9F%E5%9B%A0%EF%BC%9F.png)
这真的很烦人,systemd
只响应说我的某个服务文件的配置是错误的,但是却没有具体说明哪里错了:
/lib/systemd/system/auto_pgha.service
:
[Unit]
Description=PostgreSQL High Availability
After=network.service
After=firewalld.service
[Service]
Type=simple
WorkingDirectory=/etc/repmgr
ExecStartPre=/bin/bash -c 'echo -e "\n" `date +"%Y/%m/%d %a, %X"`": STARTING \n" >> pgha.log'
ExecStart=/bin/bash -c "python3 pg_high_availability.py &>> pgha.log"
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
在目录中,/etc/repmgr
这两个命令运行正常。但 systemd 服务仅响应错误:
# systemctl start auto_pgha
Failed to start auto_pgha.service: Unit auto_pgha.service has a bad unit file setting.
See system logs and 'systemctl status auto_pgha.service' for details.
# systemctl status -l auto_pgha
○ auto_pgha.service - PostgreSQL High Availability
Loaded: bad-setting (Reason: Unit auto_pgha.service has a bad unit file setting.)
......
auto_pgha.service: Unit configuration has fatal error, unit will not be be started.
答案1
您可以使用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.
你收到此错误是因为 systemd 本身使用了%<something>
令牌(请参阅systemd.unit(5)
手册页的“说明符”部分。
你需要写:
ExecStartPre=/bin/bash -c 'echo -e "\n" `date +"%%Y/%%m/%%d %%a, %%X"`": STARTING \n" >> pgha.log'