linux - 使 ExecStartPost 命令在后台运行

标签 linux fork systemd consul consul-template

我的 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 in ExecStartPost?

我试图在 ExecStartPost 命令的末尾添加 &,但是 consul-template 提示说这是一个额外的无效争论失败。

我还尝试将命令设为 /bin/sh -c "consul-template command here...",但这也不起作用。甚至此命令中的 nohup 也不起作用。

非常感谢任何帮助。

最佳答案

解决方法是将一个 bash 文件作为您的入口点,在其中添加您需要的所有内容,然后它就会神奇地工作

关于linux - 使 ExecStartPost 命令在后台运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40799429/

相关文章:

linux - 是否可以使用CLion调试Linux核心(转储)文件?

linux - QEMU:如何在 qemu 模拟器上使用 NFS 挂载 rootfs

systemd:覆盖单位文件时如何清除条目

linux - nginx安装出现问题,不知道如何解决

linux - 路由器上的两个 SSH (Jumphost)

子进程可以更改父进程共享的静态变量吗?

linux - 如果我有一个进程,并且我克隆了它,PID 是否相同?

c - 管理服务器对客户端的响应期间用 C 分解处理过程的服务器

Ansible - 编辑 systemd 服务文件

linux - 如何在安装之前创建安装另一个 deb 的 deb?