thin_keep_alive_service
にサービス依頼をしました/etc/init.d
。私はそれにchmod +x
権利を与えた。スクリプトは次のとおりです。
#!/bin/bash
### BEGIN INIT INFO
# Provides: thin_keep_alive_service
# Required-Start: $local_fs $network
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description Keeps Thin servers running
# Description: This service checks every 30 seconds if at least
# two light weight thin web servers are alive and
# restarts them all from the bundle if not.
###END INIT INFO
while true
do
# Store an array of the pids of thin
thin_pid_arr=($(pgrep -f thin))
# When there are less than two Thin servers left we reboot them all
if [ ${#thin_pid_arr[@]} -lt 2 ]; then
cd /root_to_app && bundle exec thin -C /etc/thin/app.yml restart
fi
#Wait 30 seconds before checking again
sleep 30
done
私がそれを実行すると、それは動作しますservice thin_keep_alive_service start
。しかし、起動した後、私のサーバーの2つはしばらくしてシャットダウンし、新しいサーバーが再起動されなかったため、バックグラウンドで実行され続けます。
バックグラウンドで実行されていることを確認するにはどうすればよいですか?
ベストアンサー1
2つ:
初期化スクリプトは少なくとも開始および停止コマンドをサポートする必要があり、初期化スクリプトに無限ループがあると、スクリプトが呼び出されると起動プロセスが中断されることがよくあります。
何をすべきかわかりませんが、
bundle exec thin restart
スクリプトが大丈夫なので問題がある可能性があります。つまり、この部分はあなたが思うように行われません。set -x
期待どおりにコマンドが実行されていることを確認するためにスクリプトを配置できます。
つまり、サービスを自動的に再起動するには、このようなプロセスマネージャを使用することがほぼ確実にきちんとなります。走る。
市。while sleep 30
代わりにfork()を使用して削除できますwhile true; do ... sleep 30; done
。 :)