supervisor进程守护

1、安装与启动

  • yum 安装
1
yum install supervisor
  • easy_install 安装
    1
    2
    3
    wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py -O - | sudo python

    easy_install supervisor
  • 配置supervisor

在etc下创建目录,并赋权限

1
mkdir -m 700 -p /etc/supervisor

在目录“ /etc/supervisor”下创建配置文件

1
echo_supervisord_conf > /etc/supervisor/supervisord.conf

修改配置文件

1
vi /etc/supervisor/supervisord.conf

在文件末尾添加,注意首尾需无空格,需顶格

1
2
[include]
files = /root/supervisorConf/*.conf

在目录“/etc/supervisor”下创建dotnet core 进程配置文件存放目录“conf.d”

1
mkdir -m 700 /root/supervisorConf

如果需要在web控制台管理,则找到以下代码

1
2
3
4
;[inet_http_server]         ; inet (TCP) server disabled by default
;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))

修改代码,删除前面的分号,port改为*:9001

1
2
3
4
[inet_http_server]         ; inet (TCP) server disabled by default
port=*:9001 ; ip_address:port specifier, *:port for all ifa
username=user ; default is no username (open server)
password=123 ; default is no password (open server)


  • 配置supervisor自动启动

    centos6

    设定supervisor启动文件
1
vi /etc/init.d/supervisord

粘贴以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! /usr/bin/env bash
# chkconfig: - 85 15

PATH=/sbin:/bin:/usr/sbin:/usr/bin

PROGNAME=supervisord

DAEMON=/usr/bin/$PROGNAME

CONFIG=/etc/supervisor/$PROGNAME.conf

PIDFILE=/tmp/$PROGNAME.pid

DESC="supervisord daemon"

SCRIPTNAME=/etc/init.d/$PROGNAME

# Gracefully exit if the package has been removed.

test -x $DAEMON || exit 0


start()

{

echo -n "Starting $DESC: $PROGNAME"

$DAEMON -c $CONFIG

echo ".............start success"

}

stop()

{

echo "Stopping $DESC: $PROGNAME"

if [ -f "$PIDFILE" ];
then
supervisor_pid=$(cat $PIDFILE)
kill -15 $supervisor_pid
echo "......"
echo "stop success"
else
echo "$DESC: $PROGNAME is not Runing"
echo ".........................stop sucess"
fi
}

status()

{ statusport=`netstat -lntp|grep 9001|awk -F ' ' '{print $4}'|awk -F ':' '{print $2}'`

if [ -f "$PIDFILE" ];
then
supervisor_pid=$(cat $PIDFILE)
echo "$DESC: $PROGNAME is Runing pid=$supervisor_pid"
else
echo "$DESC: $PROGNAME is not Runing"
echo "please use command /etc/init.d/supervisord start Run the service"
fi
}

case "$1" in

start)

start

;;

stop)

stop

;;

restart)

stop

start

;;

status)

status

;;

*)

echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2

exit 1

;;

esac

exit 0

添加自启动服务

1
chkconfig supervisord  on

启动服务

1
service supervisord start

停止服务

1
service supervisord stop

centos7

创建supervisor 自启动服务

1
vi /etc/systemd/system/supervisor.service

编辑内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=supervisor

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

使配置生效

1
systemctl daemon-reload

设置服务开机启动,即设置enable

1
systemctl enable supervisor.service

启动服务

1
systemctl start supervisor.service

2、参考配置

  • /etc/supervisor/supervisord.conf

    在文件最后添加

    1
    2
    [include]
    files = /root/supervisorConf/*.conf
  • /etc/supervisor/mongo.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [program:mongo]
    command=/home/mongodb/mongodb-linux/bin/mongod --dbpath /home/mongodb/mongodb-linux/data
    stderr_logfile=/var/log/mongo.error.log
    stdout_logfile=/var/log/mongo.stdout.log
    environment=ASPNETCORE_ENVIRONMENT=Production
    user=root
    stopsignal=INT
    autostart=true
    autorestart=true
    redirect_stderr=true
    startsecs=3
  • /etc/supervisor/mysql.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [program:mysql]
    command=/usr/local/mysql/bin/mysqld_safe --defaults-file=/mysql/3306/my.cnf
    stderr_logfile=/var/log/mongo.error.log
    stdout_logfile=/var/log/mongo.stdout.log
    environment=ASPNETCORE_ENVIRONMENT=Production
    user=root
    stopsignal=INT
    autostart=true
    autorestart=true
    redirect_stderr=true
    startsecs=3

supervisor 监控的进程必须以非daemon 方式运行

如Nginx要用supervisor 管理需要在配置文件增加一行

  • /opt/nginx/conf/nginx.conf

    1
    daemon off;
  • /etc/supervisor/nginx.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [program:nginx]
    command=/usr/local/nginx/sbin/nginx
    directory=/usr/local/nginx
    stderr_logfile=/var/log/nginx.error.log
    stdout_logfile=/var/log/nginx.stdout.log
    environment=ASPNETCORE_ENVIRONMENT=Production
    user=root
    stopsignal=INT
    autostart=true
    autorestart=true
    redirect_stderr=true
    startsecs=3
  • /etc/supervisor/redis.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [program:redis]
    command=/usr/local/bin/redis-server /opt/redis/redis.conf
    stderr_logfile=/var/log/Redis.error.log
    stdout_logfile=/var/log/Redis.stdout.log
    environment=ASPNETCORE_ENVIRONMENT=Production
    user=root
    stopsignal=INT
    autostart=true
    autorestart=true
    redirect_stderr=true
    startsecs=3

3、其他

更新新的配置到supervisord:

1
supervisorctl update

更新新的配置到supervisord

1
supervisorctl update

重新启动配置中的所有程序

1
supervisorctl reload

启动某个进程(program_name=你配置中写的程序名称)

1
supervisorctl start program_name

查看正在守候的进程

1
supervisorctl

停止某一进程 (program_name=你配置中写的程序名称)

1
pervisorctl stop program_name

重启某一进程 (program_name=你配置中写的程序名称)

1
supervisorctl restart program_name

停止全部进程

1
supervisorctl stop all
Critas wechat
欢迎您扫一扫上面的二维码,订阅我的从不更新的公众号!
坚持原创技术分享,您的支持将鼓励我继续创作!