大网数据广告活动
服务器运维问答

How can I find the exact cause of systemd service unable to start (service has a bad unit file setting)?

· 52CE 我爱测

This is really annoying that systemd only responded that my configuration of a service file is wrong, but does not specify where is wrong exactly: /lib/systemd/system/auto_pgha.service : [Unit] Description=PostgreSQL High Availab…

问题原文

原帖链接 · George Y · CC BY-SA 4.0

This is really annoying that systemd only responded that my configuration of a service file is wrong, but does not specify where is wrong exactly:

/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

Inside directory /etc/repmgr the two commands worked just fine. But the systemd service just responded with an error:

# 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. 

已采纳解答

原帖链接 · larsks · CC BY-SA 4.0

You can use the systemd-analyze verify command. If we put the content from your question into pgha.service, we see:

$ 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.

You're getting this error because systemd itself uses %<something> tokens (see the "Specifiers" section of the systemd.unit(5) man page.

You would need to write:

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-09-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。