【问题标题】:Make ExecStartPost command to run in background使 ExecStartPost 命令在后台运行
【发布时间】:2016-11-25 07:05:34
【问题描述】:

我的 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 回来,我的应用程序和领事模板进程都在运行。

有没有办法在ExecStartPost指定的后台运行consul-template进程?

我试图在ExecStartPost 命令的末尾添加&,但随后consul-template 抱怨这是一个额外的无效参数并且失败了。

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

非常感谢任何帮助。

【问题讨论】:

  • consul-template 进程分离到它自己的systemd 单元可能会更成功。

标签: linux fork systemd consul consul-template


【解决方案1】:

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

【讨论】:

  • 我最终为 consul-template 创建了一个单独的服务,并使用 consul 的键值对创建了一个自渲染模板文件。
  • 领事真棒! :) 如果您认为我确实以某种方式提供了帮助,请评价 :)
  • 我将提交我的作为答案。它对很多人都有用。
猜你喜欢
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多