我有一个systemd
服务,用于连接到consul
服务器的 Spring Boot 应用程序,位于haproxy
. consul
提供通过命令consul-template
自动更新haproxy
配置文件中的服务位置。consul-template
consul-template
获取一个模板文件并写入最终的 haproxy 配置文件,然后重新加载haproxy
.
现在,consul-template
进程需要始终与我的应用程序一起在后台运行,这样当应用程序启动时,它可以检测到新的应用程序启动并更新其在配置文件中的位置。
这是我的systemd
服务文件。
[Unit]
Description=myservice
Requires=network-online.target
After=network-online.target
[Service]
Type=forking
PIDFile=/home/dragon/myservice/run/myservice.pid
ExecStart=/home/dragon/myservice/bin/myservice-script start
ExecReload=/home/dragon/myservice/bin/myservice-script reload
ExecStop=/home/dragon/myservice/bin/myservice-script stop
ExecStartPost=consul-template -template '/etc/haproxy/haproxy.cfg.template:/etc/haproxy/haproxy.cfg:sudo systemctl reload haproxy'
User=dragon
[Install]
WantedBy=multi-user.target
现在,当我 start 时systemctl start myservice
,我的应用程序启动并且调用consul-template
也可以工作,但consul-template
进程不会进入后台。我必须按下Ctl+C
然后systemctl
回来,我的应用程序和领事模板进程都在运行。
有没有办法
consul-template
在指定的后台运行进程ExecStartPost
?
我试图&
在命令末尾添加ExecStartPost
,但随后consul-template
抱怨它是一个额外的无效参数并且它失败了。
我也试图将命令设为/bin/sh -c "consul-template command here..."
,但这也不起作用。即使nohup
在这个命令中也不起作用。
非常感谢任何帮助。