2016-11-25 49 views
0

運行我有一個systemd服務連接到服務器consul我春天啓動的應用程序,背後haproxyconsul提供consul-template經由consul-template命令自動更新haproxy配置文件中的服務位置。讓ExecStartPost命令在後臺

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在此命令中也不起作用。

任何幫助非常感謝。

+0

你可能與'領事template'過程中分離到自己更成功系統單元。 – Amir

回答

1

一種解決方法是將有一個bash文件作爲入口點,加入所有你有需要的話,都將神奇的工作

+0

我最終爲consul-template創建了一個單獨的服務,並使用consul的鍵值對創建了一個自我渲染模板文件。 – divinedragon

+0

領事真棒! :)率,如果你認爲我實際上在某種方式幫助:) –

+0

我將提交我的答案。這對很多人都有用。 – divinedragon