如何找到 systemd 服務無法啟動的確切原因(服務的單元檔案設定錯誤)?

如何找到 systemd 服務無法啟動的確切原因(服務的單元檔案設定錯誤)?

這真的很煩人,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'

相關內容