我的 spring boot 应用程序有一个 systemd
服务连接到 consul
服务器,在 haproxy
后面。 consul
提供consul-template
通过consul-template
命令自动更新haproxy
配置文件中的服务位置。
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
现在,当我启动 systemctl start myservice
时,我的应用程序启动并且对 consul-template
的调用也有效,但是 consul-template
进程不会进入后台。我必须按 Ctl+C
,然后 systemctl
返回,我的应用程序和 consul-template 进程都在运行。
Is there way to run the
consul-template
process in background specified inExecStartPost
?
我试图在 ExecStartPost
命令的末尾添加 &
,但是 consul-template
提示说这是一个额外的无效争论失败。
我还尝试将命令设为 /bin/sh -c "consul-template command here..."
,但这也不起作用。甚至此命令中的 nohup
也不起作用。
非常感谢任何帮助。
最佳答案
解决方法是将一个 bash 文件作为您的入口点,在其中添加您需要的所有内容,然后它就会神奇地工作
关于linux - 使 ExecStartPost 命令在后台运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40799429/