2016-01-05 109 views
5

我創建了一個systemd服務,它應該在啓動或重啓時調用shell腳本。爲什麼systemd在啓動後立即停止服務?

[Unit] 
Description=Starts the DCCA index software 

[Install] 
WantedBy=multi-user.target 

[Service] 
ExecStart=/opt/insiteone/bin/indexControl start 
ExecStop=/opt/insiteone/bin/indexControl stop 

# Execute pre and post scripts as root 
#PermissionsStartOnly=true 
Restart=on-abort 
TimeoutSec=600 

最初,它不停地只要它開始重新啓動在無限循環,但是當我加入TimeoutSec選項,它只要服務已啓動首次(開始叫ExecStop,然後立即再次停止)。

任何線索,我哪裏會出錯? P.S:indexControl是一個shell腳本,它啓動其他進程。

+0

'chkconfig index off'也沒有幫助(index.service是ssystemd服務文件) – kingsmasher1

+0

我在這裏得到了答案:http://superuser.com/questions/1022142/why-is-systemd-stopping -service-immediately-after-it-is-started修復了問題 – kingsmasher1

回答

3

嘗試改變Restart=on-abortRestart=on-abnormal

http://www.freedesktop.org/software/systemd/man/systemd.service.html

這個設置對故障是建議選擇長期運行 服務,以便通過嘗試從自動 復甦以增加可靠性錯誤。對於能夠終止於 自己選擇的服務(並且避免立即重新啓動),對於異常是 的替代選擇。

此外,您可能需要將Type=oneshot添加到[Service]部分。

https://wiki.archlinux.org/index.php/Systemd#Service_types

類型=單衝:這是爲做一個工作,然後 退出腳本很有用。您可能還需要設置RemainAfterExit = yes,以便systemd 在該進程退出後仍將該服務視爲活動。

您可以粘貼下面我推薦的變化:

[Unit] 
Description=Starts the DCCA index software 

[Install] 
WantedBy=multi-user.target 

[Service] 
Type=oneshot 
ExecStart=/opt/insiteone/bin/indexControl start 
ExecStop=/opt/insiteone/bin/indexControl stop 
Restart=on-abnormal 

別的東西要考慮的是你是否甚至不需要Restart=線...請問這個腳本文件服務電話經常失敗?

+5

感謝您的回答,但type = oneshot'不起作用,但是'type = forking'。我從這裏得到了答案,這很有效。 http://superuser.com/questions/1022142/why-is-systemd-stopping-service-immediately-after-it-is-started/ – kingsmasher1

+0

With RemainAfterExit = yes and Type = oneshot也許可以工作 – AkisC

+1

無法設置Restart = on在** oneshot **服務上的「異常」,只允許'Restart = no'被允許: '服務有重啓=設置不是no,Type = oneshot服務不允許。 Refusing.' – s1moner3d