superviosrは、Linux / Unixシステムのプロセス監視ツールです。彼/彼女のスーパーバイザーは、Pythonによって開発された一般的なプロセス管理プログラムであり、Linux上のプロセスを管理および監視し、一般的なコマンドラインプロセスをバックグラウンドデーモンに変えることができます。また、プロセスのステータスを監視すると、異常終了時に自動的に再起動できます。ただし、daemontoolsと同様に、デーモンプロセス(つまり、バックグラウンドプロセス)を監視することはできません
apt-get install -y supervisor
インストールが成功すると、 supervisord.conf
構成ファイルが / etc / supervisor
ディレクトリに生成されます。
echo_supervisord_conf> Supervisord.conf
コマンドを使用して、デフォルトの構成ファイルを生成することもできます(推奨されません。より多くのコンテンツがあります)。
Supervisord.confの設定例:
; supervisor config file
[ unix_http_server]
file=/var/run/supervisor.sock ;(the path to the socket file)
chmod=0700; sockef file mode(default0700)[supervisord]
logfile=/var/log/supervisor/supervisord.log ;(main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ;(supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ;('AUTO' child log dir,default $TEMP); the below section must remain in the config file for RPC
;( supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[ rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[ supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket; The [include] section can just contain the "files" setting. This
; setting can list multiple files(separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*; include files themselves.[include]
files =/etc/supervisor/conf.d/*.conf
プロセス構成は、 / etc / supervisor / conf.d
ディレクトリにある * .conf
構成ファイルを読み取ります。
インストールが完了すると、デフォルトでスーパーバイザーが開始されます
前回の記事では、リンクは次のとおりです。
https://www.cnblogs.com/xiao987334176/p/11329906.html
Uwsgiとnginxが構成されました。これらは2つの重要なプロセスです。いずれかが停止すると、Webページにアクセスできなくなります。
バックグラウンド操作をオフにします、なぜですか?監視対象はバックグラウンドプロセスを管理できないため
cd /www/mysite1/uwsgi
vim uwsgi.ini
デーモン化をコメントアウト
[ uwsgi]
# Django-related settings
# the base directory(full path)
chdir =/www/mysite1
# Django's wsgi file
module = mysite1.wsgi
# the virtualenv(full path)
home =/virtualenvs/venv
# process-related settings
# master
master =true
# maximum number of worker processes
processes =1
# pid file
pidfile =/www/mysite1/uwsgi/uwsgi.pid
# socket file path(full path)
socket =/www/mysite1/uwsgi/mysite1.sock
# clear environment on exit
vacuum =true
# The process runs in the background and types the log to the specified log file
# daemonize =/www/mysite1/uwsgi/uwsgi.log
uwsgiを閉じる
/virtualenvs/venv/bin/uwsgi --stop uwsgi.pid
cd /etc/supervisor/conf.d
vim uwsgi.conf
内容は以下の通りです。
[ program:uwsgi]
directory =/www/mysite1 ;プログラムの開始ディレクトリ
command=/virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini ;開始コマンド
autostart =true;監視対象の開始時に自動的に開始
startsecs =5;起動から5秒経過しても異常終了がない場合は正常に起動したものとみなします。
autorestart =true;プログラムが異常終了した後、自動的に再起動します
startretries =3;起動失敗時の自動再試行回数。デフォルトは3です。
user = root ;どのユーザーから始めるか
redirect_stderr =true;stderrをstdoutにリダイレクトします。デフォルトはfalseです。
stdout_logfile_maxbytes = 20MB ;stdoutログファイルサイズ、デフォルトは50MB
stdout_logfile_backups =20;stdoutログファイルのバックアップ番号
; stdoutログファイルの場合、指定したディレクトリが存在しないと正常に起動できないため、手動でディレクトリを作成する必要があります。(Supervisordは自動的にログファイルを作成します)
stdout_logfile =/www/mysite1/logs/stdout.log
; エラーファイルを出力
stderr_logfile =/www/mysite1/logs/stderr.log
; 操作に必要な環境変数を追加します,仮想環境
; environment=PYTHONPATH=$PYTHONPATH:/virtualenvs/venv/bin/;次に、メインプロセスを強制終了した後、子プロセスも停止できることを確認します
stopasgroup=true
killasgroup=true
ログディレクトリを作成します
mkdir /www/mysite1/logs/
**注:supervisordはディレクトリを自動的に作成しないため、手動で作成する必要があります。 ****
supervisorctl reload
表示された場合:
error:<class'socket.error'>,[Errno 2] No such file or directory: file:/usr/lib/python2.7/socket.py line:228
最初にプロセスが存在するかどうかを確認します:ps -ef | grep Supervisord次に、コマンドsupervisord -c / etc / supervisor / supervisord.confを使用してプロセスを開始します。
最初のビュー、状態はSTARTING、2番目のビュー、状態はRUNNINGです
root@ubuntu:/etc/supervisor/conf.d# supervisorctl status
uwsgi STARTING
root@ubuntu:/etc/supervisor/conf.d# supervisorctl status
uwsgi RUNNING pid 20367, uptime 0:00:12
root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep uwsgi
root 203670.30.810268034832? S 13:500:00/virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root 203690.00.710268028768? S 13:500:00/virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root 203770.00.015984976 pts/1 S+13:520:00 grep --color=auto uwsgi
root@ubuntu:/etc/supervisor/conf.d# killall -9 uwsgi
root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep uwsgi
root 203790.00.810267634844? S 13:520:00/virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root 203810.00.710267628488? S 13:520:00/virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root 203830.00.015984968 pts/1 S+13:520:00 grep --color=auto uwsgi
上記の情報を見つけることができます。プロセスが強制的に強制終了された後、スーパーバイザーはuwsgiを開始します。
スーパーバイザーはバックグラウンドプログラムを監視できないため、
command = / usr / local / bin / nginxこのコマンドは、デフォルトでバックグラウンドで開始されます。
-g'daemon off; 'を追加します。このパラメーターはこの問題を解決できます。このパラメーターは、フォアグラウンドで実行することを意味します。
command = /usr/local/bin/nginx -g ‘daemon off;’
cd /etc/supervisor/conf.d
vim nginx.conf
内容は以下の通りです。
[ program:nginx]
command =/usr/sbin/nginx -g 'daemon off;'
startsecs=0
autostart=true
autorestart=true
stdout_logfile=/var/log/nginx/stdout.log
stopasgroup=true
killasgroup=true
構成のロード
supervisorctl reload
ステータスを確認する
root@ubuntu:/etc/supervisor/conf.d# supervisorctl status
nginx RUNNING pid 20409, uptime 0:00:00uwsgi RUNNING pid 20404, uptime 0:00:07
nginxプロセスを強制終了します
root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep nginx
root 204410.20.21251169832? S 13:580:00 nginx: master process /usr/sbin/nginx -g daemon off;
www-data 204420.00.01254403228? S 13:580:00 nginx: worker process
root 204460.00.0159841084 pts/1 S+13:580:00 grep --color=auto nginx
root@ubuntu:/etc/supervisor/conf.d# killall -9 nginx
root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep nginx
root 204480.00.21251169792? S 13:580:00 nginx: master process /usr/sbin/nginx -g daemon off;
www-data 204490.00.01254403132? S 13:580:00 nginx: worker process
root 204510.00.015984936 pts/1 S+13:580:00 grep --color=auto nginx
この記事の参照:
https://www.cnblogs.com/xishuai/p/ubuntu-install-supervisor.html