
我有一個應用程式需要在作業系統啟動時自動啟動。我在 CentOS 6 中運行,因此我使用放置在/etc/init.d
.到目前為止,這一切都很好並且有效。
現在......除此之外,我想要的是,如果我的應用程式崩潰(錯誤退出),我希望它能夠自動再次啟動。理想情況下,甚至可以定義最大嘗試次數。在 Mac 的 Info.plist 中有一些按鍵,KeepAlive
因為我已經開始chkconfig
工作了,所以我想知道是否可以用它做點什麼。
我知道有http://mmonit.com/monit/但對於我的需要來說可能太多了。
答案1
如果您控制 init.d 腳本來啟動此進程,您可能只想將進程的執行包裝在 init.d 腳本中,如下所示:
until myserver; do
echo "Server 'myserver' crashed with exit code $?. Respawning.." >&2
sleep 1
done
這將使您的進程基本上永遠處於直到循環中,每次它終止時。計數等可以這樣引入:
cnt=0
max=3
until myserver; do
let cnt=cnt+1
echo "Server 'myserver' crashed with exit code $?. Respawning.." >&2
sleep 1
[ $cnt = $max ] && exit;
done