CentOS6.9 Supervisorをインストールしてバックグラウンドプロセスを管理し、起動を設定する
23785 ワード
このノートにはCentOS 6が記録されている.9にSupervisorをインストールしてバックグラウンドのプロセスを管理して、そしてSupervisordをサービスに参加して、電源を入れて起動することを設定して、サービスを通じて管理する過程
ソース・ストレージ・ディレクトリの作成とアクセス
supervisorソースコードのダウンロードと解凍
supervisorのインストール
エラーが発生した場合:
supervisordを作成します.dディレクトリ
プロファイルの作成と編集
以下の箇所を修正する
supervisordの起動
プロセスの表示
起動スクリプトの作成
次の構成を書き込みます.
サービスに参加する
権限の設定
起動
テストphpファイルの作成
次のコードを書き込み、プロセスを開始するときに10秒休止し、logを書き込むとすぐに終了します.
プロジェクトプロセス管理構成の作成
次の構成を書き込みます.
supervisorの起動
supervisorの実行ステータスの表示
supervisorログの表示
testを表示します.phpによるlog
以上のログから、phpのプロセスはlogに書き込まれた後に終了し、supervisorはphpプロセスが終了した後に再びプロセス原文アドレスを起動することがわかります.https://www.jmsite.cn/blog-635.html
ソース・ストレージ・ディレクトリの作成とアクセス
mkdir -p /usr/local/src
cd /usr/local/src
supervisorソースコードのダウンロードと解凍
wget -c https://files.pythonhosted.org/packages/ba/65/92575a8757ed576beaee59251f64a3287bde82bdc03964b89df9e1d29e1b/supervisor-3.3.5.tar.gz
tar -zxvf supervisor-3.3.5.tar.gz
cd supervisor-3.3.5
supervisorのインストール
python setup.py install
エラーが発生した場合:
ImportError: No module named setuptools
python-setuptools yum install python-setuptools
のインストールの結果はsupervisorのインストールに成功したことを示します.Installed /usr/lib/python2.6/site-packages/meld3-1.0.2-py2.6.egg
Finished processing dependencies for supervisor==3.3.5
supervisordを作成します.dディレクトリ
mkdir /etc/supervisord.d
プロファイルの作成と編集
echo_supervisord_conf > /etc/supervisord.conf
vim /etc/supervisord.conf
以下の箇所を修正する
file=/var/run/supervisor.sock
logfile=/var/log/supervisord.log
pidfile=/var/run/supervisord.pid
serverurl=unix:///var/run/supervisor.sock
[include]
files = supervisord.d/*.conf
supervisordの起動
supervisord -c /etc/supervisord.conf
プロセスの表示
ps aux | grep supervisord
root 3110 0.0 1.0 196612 10816 ? Ss 20:12 0:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root 3504 0.0 0.0 103336 908 pts/0 S+ 20:19 0:00 grep supervisord
kill 3110
起動スクリプトの作成
vim /etc/init.d/supervisord
次の構成を書き込みます.
#!/bin/bash
#
# supervisord This scripts turns supervisord on
#
# Author: Mike McGrath (based off yumupdatesd)
# Jason Koppe adjusted to read sysconfig,
# use supervisord tools to start/stop, conditionally wait
# for child processes to shutdown, and startup later
# Mikhail Mingalev Merged
# redhat-init-jkoppe and redhat-sysconfig-jkoppe, and
# made the script "simple customizable".
# Brendan Maguire Added OPTIONS to
# SUPERVISORCTL status call
#
# chkconfig: 345 83 04
#
# description: supervisor is a process control utility. It has a web based
# xmlrpc interface as well as a few other nifty features.
# Script was originally written by Jason Koppe .
#
# source function library
. /etc/rc.d/init.d/functions
set -a
PREFIX=/usr
SUPERVISORD=$PREFIX/bin/supervisord
SUPERVISORCTL=$PREFIX/bin/supervisorctl
PIDFILE=/var/run/supervisord.pid
LOCKFILE=/var/lock/subsys/supervisord
OPTIONS="-c /etc/supervisord.conf"
# unset this variable if you don't care to wait for child processes to shutdown before removing the $LOCKFILE-lock
WAIT_FOR_SUBPROCESSES=yes
# remove this if you manage number of open files in some other fashion
ulimit -n 96000
RETVAL=0
running_pid()
{
# Check if a given process pid's cmdline matches a given name
pid=$1
name=$2
[ -z "$pid" ] && return 1
[ ! -d /proc/$pid ] && return 1
(cat /proc/$pid/cmdline | tr "\000" "
"|grep -q $name) || return 1
return 0
}
running()
{
# Check if the process is running looking at /proc
# (works for all users)
# No pidfile, probably no daemon present
[ ! -f "$PIDFILE" ] && return 1
# Obtain the pid and check it against the binary name
pid=`cat $PIDFILE`
running_pid $pid $SUPERVISORD || return 1
return 0
}
start() {
echo "Starting supervisord: "
if [ -e $PIDFILE ]; then
echo "ALREADY STARTED"
return 1
fi
# start supervisord with options from sysconfig (stuff like -c)
$SUPERVISORD $OPTIONS
# show initial startup status
$SUPERVISORCTL $OPTIONS status
# only create the subsyslock if we created the PIDFILE
[ -e $PIDFILE ] && touch $LOCKFILE
}
stop() {
echo -n "Stopping supervisord: "
$SUPERVISORCTL $OPTIONS shutdown
if [ -n "$WAIT_FOR_SUBPROCESSES" ]; then
echo "Waiting roughly 60 seconds for $PIDFILE to be removed after child processes exit"
for sleep in 2 2 2 2 4 4 4 4 8 8 8 8 last; do
if [ ! -e $PIDFILE ] ; then
echo "Supervisord exited as expected in under $total_sleep seconds"
break
else
if [[ $sleep -eq "last" ]] ; then
echo "Supervisord still working on shutting down. We've waited roughly 60 seconds, we'll let it do its thing from here"
return 1
else
sleep $sleep
total_sleep=$(( $total_sleep + $sleep ))
fi
fi
done
fi
# always remove the subsys. We might have waited a while, but just remove it at this point.
rm -f $LOCKFILE
}
restart() {
stop
start
}
case "$1" in
start)
start
RETVAL=$?
;;
stop)
stop
RETVAL=$?
;;
restart|force-reload)
restart
RETVAL=$?
;;
reload)
$SUPERVISORCTL $OPTIONS reload
RETVAL=$?
;;
condrestart)
[ -f $LOCKFILE ] && restart
RETVAL=$?
;;
status)
$SUPERVISORCTL $OPTIONS status
if running ; then
RETVAL=0
else
RETVAL=1
fi
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
exit 1
esac
exit $RETVAL
サービスに参加する
chkconfig --add supervisord
権限の設定
chmod 755 /etc/init.d/supervisord
起動
chkconfig supervisord on
テストphpファイルの作成
vim /www/test.php
次のコードを書き込み、プロセスを開始するときに10秒休止し、logを書き込むとすぐに終了します.
sleep(10);
error_log(date('Y-m-d H:i:s').":".getmypid()."
",3,'/var/log/test.php.log');
プロジェクトプロセス管理構成の作成
vim /etc/supervisord.d/jmsite.conf
次の構成を書き込みます.
[program:jmsite]
command=/usr/bin/php /www/test.php
process_name=%(program_name)s_%(process_num)02d
autorestart=true
autostart=true
numprocs=3
startsecs=1
stderr_logfile=/var/log/supervisor.err.log
stdout_logfile=/var/log/supervisor.out.log
supervisorの起動
service supervisord start
Starting supervisord:
jmsite:jmsite_00 STARTING
jmsite:jmsite_01 STARTING
jmsite:jmsite_02 STARTING
supervisorの実行ステータスの表示
service supervisord status
jmsite:jmsite_00 RUNNING pid 7096, uptime 0:01:38
jmsite:jmsite_01 RUNNING pid 7097, uptime 0:01:38
jmsite:jmsite_02 RUNNING pid 7098, uptime 0:01:38
supervisorログの表示
tail -f /var/log/supervisord.log
2019-01-27 21:45:42,031 INFO success: jmsite_02 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-27 21:45:48,022 INFO exited: jmsite_00 (exit status 0; expected)
2019-01-27 21:45:49,025 INFO spawned: 'jmsite_00' with pid 8105
2019-01-27 21:45:50,023 INFO exited: jmsite_01 (exit status 0; expected)
2019-01-27 21:45:51,026 INFO success: jmsite_00 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-27 21:45:51,027 INFO spawned: 'jmsite_01' with pid 8106
2019-01-27 21:45:51,045 INFO exited: jmsite_02 (exit status 0; expected)
2019-01-27 21:45:52,046 INFO success: jmsite_01 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-27 21:45:52,047 INFO spawned: 'jmsite_02' with pid 8107
2019-01-27 21:45:53,050 INFO success: jmsite_02 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-27 21:45:59,040 INFO exited: jmsite_00 (exit status 0; expected)
2019-01-27 21:46:00,043 INFO spawned: 'jmsite_00' with pid 8111
2019-01-27 21:46:01,043 INFO success: jmsite_00 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-27 21:46:01,045 INFO exited: jmsite_01 (exit status 0; expected)
2019-01-27 21:46:02,048 INFO spawned: 'jmsite_01' with pid 8112
2019-01-27 21:46:02,063 INFO exited: jmsite_02 (exit status 0; expected)
2019-01-27 21:46:03,064 INFO success: jmsite_01 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-01-27 21:46:03,065 INFO spawned: 'jmsite_02' with pid 8113
2019-01-27 21:46:04,067 INFO success: jmsite_02 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
......
testを表示します.phpによるlog
tail -f /var/log/test.php.log
2019-01-27 21:45:14:8052
2019-01-27 21:45:15:8053
2019-01-27 21:45:17:8087
2019-01-27 21:45:25:8088
2019-01-27 21:45:26:8089
2019-01-27 21:45:29:8092
2019-01-27 21:45:36:8093
2019-01-27 21:45:39:8096
2019-01-27 21:45:40:8097
2019-01-27 21:45:48:8100
2019-01-27 21:45:50:8101
2019-01-27 21:45:51:8102
2019-01-27 21:45:59:8105
2019-01-27 21:46:01:8106
2019-01-27 21:46:02:8107
......
以上のログから、phpのプロセスはlogに書き込まれた後に終了し、supervisorはphpプロセスが終了した後に再びプロセス原文アドレスを起動することがわかります.https://www.jmsite.cn/blog-635.html