Winse Blog

走走停停都是风景, 熙熙攘攘都向最好, 忙忙碌碌都为明朝, 何畏之.

Supervisor安装配置

2019-7-27

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
yum install epel-release

yum install python-pip
# OR
#yum install python-setuptools
#easy_install pip

pip --version

# Python3
#yum search pip | grep python3
#pip3 -V

[root@node ~]# pip install supervisor
Collecting supervisor
  Downloading https://files.pythonhosted.org/packages/a5/27/03ee384818f4fc5f678743bb20ac49c5b4fc9f531bd404dec4b61a8b5d42/supervisor-4.0.4-py2.py3-none-any.whl (296kB)
    100% |████████████████████████████████| 307kB 78kB/s 
Collecting meld3>=1.0.0 (from supervisor)
  Downloading https://files.pythonhosted.org/packages/b6/ae/e6d731e4b9661642c1b20591d8054855bb5b8281cbfa18f561c2edd783f7/meld3-1.0.2-py2.py3-none-any.whl
Installing collected packages: meld3, supervisor
Successfully installed meld3-1.0.2 supervisor-4.0.4

#http://supervisord.org/installing.html#creating-a-configuration-file
echo_supervisord_conf > /etc/supervisord.conf
mkdir /etc/supervisord.d/ 

vi /etc/supervisord.conf
[inet_http_server]
port=*:9001
username=test
password=111111

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
loglevel=info                ; log level; default info; others: debug,warn,trace
pidfile=/var/run/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false               ; start in foreground if true; default false
minfds=1024                  ; min. avail startup file descriptors; default 1024
minprocs=200                 ; min. avail process descriptors;default 200

[include]
files = /etc/supervisord.d/*.conf

#http://supervisord.org/running.html#running
supervisord -c /etc/supervisord.conf

lsof -i:9001

vi /etc/supervisord.d/crawler.conf
[program:crawler]
command=java -Dspring.profiles.active=crawlerproxypro -Dnode.id=local-1 -jar weibo-crawler-1.0-boot.jar
directory=/root/weibo                          ; 执行前要不要先cd到目录去,一般不用
priority=1                                     ; 数字越高,优先级越高
numprocs=1                                     ; 启动几个进程
autostart=true                                 ; 随着supervisord的启动而启动
autorestart=unexpected                         ; 自动重启。。当然要选上了
startretries=3                                ; 启动失败时的最多重试次数
exitcodes=0                                    ; 正常退出代码(是说退出代码是这个时就不再重启了吗?待确定)
stopsignal=KILL                                ; 用来杀死进程的信号
stopwaitsecs=10                                ; 发送SIGKILL前的等待时间
redirect_stderr=true                           ; 重定向stderr到stdout
stdout_logfile = /var/log/supervisor/%(program_name)s_%(process_num)02d.log
stderr_logfile = /var/log/supervisor/crawler-stderr.log
loglevel=error ; http://supervisord.org/logging.html#activity-log-levels
stdout_logfile_maxbytes=0
; stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
; command = sh -c "sleep 5;date >>/root/test.ts"

supervisorctl reload
# OR
#supervisorctl -c /etc/supervisord.conf

wget https://raw.githubusercontent.com/Supervisor/initscripts/master/centos-systemd-etcs -O /usr/lib/systemd/system/systemd-supervisor.service

systemctl enable supervisord.service
systemctl start supervisord.service

2017

supervisor使用ini的方式配置其实挺讨厌的,但有一点好的就是它不需要配置为boot进程(process id 1)。

在线安装

下载 https://pypi.python.org/pypi/supervisor ,并安装:

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
# yum的supervisor版本太低(2.1)了,使用tar.gz源码来安装
[root@cu2 supervisor-3.2.3]# yum list supervisor
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
 * epel: ftp.cuhk.edu.hk
Available Packages
supervisor.noarch                                                                              2.1-9.el6                                                                               epel


# 不支持python3,安装之前先保证安装有python2.4+
[root@cu2 ~]# python --version
Python 2.6.6
[root@cu2 ~]# cd supervisor-3.2.3/
[root@cu2 supervisor-3.2.3]# python setup.py install
...
creating dist
creating 'dist/supervisor-3.2.3-py2.6.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing supervisor-3.2.3-py2.6.egg
creating /usr/lib/python2.6/site-packages/supervisor-3.2.3-py2.6.egg
Extracting supervisor-3.2.3-py2.6.egg to /usr/lib/python2.6/site-packages
Adding supervisor 3.2.3 to easy-install.pth file
Installing echo_supervisord_conf script to /usr/bin
Installing pidproxy script to /usr/bin
Installing supervisorctl script to /usr/bin
Installing supervisord script to /usr/bin

Installed /usr/lib/python2.6/site-packages/supervisor-3.2.3-py2.6.egg
Processing dependencies for supervisor==3.2.3
Searching for meld3>=0.6.5
Reading http://pypi.python.org/simple/meld3/
Best match: meld3 1.0.2
Downloading https://pypi.python.org/packages/45/a0/317c6422b26c12fe0161e936fc35f36552069ba8e6f7ecbd99bbffe32a5f/meld3-1.0.2.tar.gz#md5=3ccc78cd79cffd63a751ad7684c02c91
Processing meld3-1.0.2.tar.gz
Running meld3-1.0.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-nMSEmq/meld3-1.0.2/egg-dist-tmp-vkwrOp
zip_safe flag not set; analyzing archive contents...
Adding meld3 1.0.2 to easy-install.pth file

Installed /usr/lib/python2.6/site-packages/meld3-1.0.2-py2.6.egg
Finished processing dependencies for supervisor==3.2.3

(离线安装)下载依赖以及安装包,并安装

安装官网的版本下载: Installing To A System Without Internet Access

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
python -V

tar zxvf setuptools-18.2.tar.gz 
cd setuptools-18.2
python setup.py install

tar zxvf meld3-0.6.5.tar.gz 
cd meld3-0.6.5
python setup.py install

tar zxvf elementtree-1.2.6-20050316.tar.gz 
cd elementtree-1.2.6-20050316
python setup.py install

tar zxvf supervisor-3.1.3.tar.gz 
cd supervisor-3.1.3
python setup.py  install

启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# http://supervisord.org/installing.html#creating-a-configuration-file
[root@cu2 supervisor-3.2.3]# echo_supervisord_conf >/etc/supervisord.conf
[root@cu2 supervisor-3.2.3]# supervisord
/usr/lib/python2.6/site-packages/supervisor-3.2.3-py2.6.egg/supervisor/options.py:296: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
  'Supervisord is running as root and it is searching '

[root@cu2 supervisor-3.2.3]# ps aux | grep supervisor | grep -v grep
root     63599  0.0  0.1 196600 10404 ?        Ss   16:57   0:00 /usr/bin/python /usr/bin/supervisord

[root@cu2 supervisor-3.2.3]# supervisorctl 
supervisor> help

default commands (type help <topic>):
=====================================
add    exit      open  reload  restart   start   tail   
avail  fg        pid   remove  shutdown  status  update 
clear  maintail  quit  reread  signal    stop    version

supervisor> pid
63599
supervisor> shutdown
Really shut the remote supervisord process down y/N? y
Shut down
supervisor> exit

ctl命令查看状态:

  • reread: Reload the daemon’s configuration files
  • update: Reload config and add/remove as necessary
  • reload: Restart the remote supervisord.
  • pid: supervisord的进程号
  • status: Get all process status info
  • avail: Display all configured processes

网页管理:

配置

http://supervisord.org/configuration.html

默认server(supervisord)-client(supervisorctl)通过 unix domain socket 文件的方式来通信,为了方便网页查看同时开启web配置 inet_http_server

program区域的配置要细读,主要配置工作都在这个上面。被管理的程序 不能后台运行 (例如:java程序不要加 nohup 以及 & )!!

Controlled programs should themselves not be daemons, as supervisord assumes it is responsible for daemonizing its subprocesses

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
[root@cu2 supervisor-3.2.3]# vi /etc/supervisord.conf 
...
[inet_http_server]         ; inet (TCP) server disabled by default
port=0.0.0.0:9001        ; (ip_address:port specifier, *:port for all iface)
...
[include]
files = /etc/supervisord.d/*.ini


[root@cu2 ~]# cat /etc/supervisord.d/redis.ini
[program:redis]
command=/home/hadoop/redis/bin/redis-server /home/hadoop/redis/redis.conf --port 1637%(process_num)01d
process_name=%(program_name)s_1637%(process_num)01d
numprocs=4
numprocs_start=0
priority=1
autostart=true
startsecs=0
startretries=3
autorestart=true
directory=/home/hadoop/redis

[root@cu2 ~]# supervisorctl shutdown
Shut down
[root@cu2 ~]# 
[root@cu2 ~]# supervisord

[root@cu2 ~]# ps aux | grep redis
root     50458  0.1  0.0 137444  2384 ?        Sl   18:14   0:00 /home/hadoop/redis/bin/redis-server *:16372                                   
root     50460  0.0  0.0 137444  2380 ?        Sl   18:14   0:00 /home/hadoop/redis/bin/redis-server *:16373                                   
root     50461  0.0  0.0 137444  2384 ?        Sl   18:14   0:00 /home/hadoop/redis/bin/redis-server *:16370                                   
root     50462  0.1  0.0 137444  2388 ?        Sl   18:14   0:00 /home/hadoop/redis/bin/redis-server *:16371                                   
root     51853  0.0  0.0 103248   888 pts/2    S+   18:14   0:00 grep redis

# 测试:随便kill掉一个,再ps查看,进程没少,但是刚刚kill掉的redis-server进程号变了

# 可以通过supervisord.log查看启动的日志。默认在/tmp下面
[root@cu2 tmp]# less supervisord.log 

# 如果在 配置program块 没有指定stdout和stderr的位置,可以在/tmp下找到对应的日志文件:
[root@cu2 tmp]# ll | grep redis
-rw------- 1 root   root      0 May  2 18:14 redis_16370-stderr---supervisor-pZ5pzl.log
-rw------- 1 root   root   2649 May  2 18:17 redis_16370-stdout---supervisor-GyrHYw.log
-rw------- 1 root   root      0 May  2 18:14 redis_16371-stderr---supervisor-XfyvMv.log
-rw------- 1 root   root   5298 May  2 18:20 redis_16371-stdout---supervisor-NOmTQJ.log
-rw------- 1 root   root      0 May  2 18:14 redis_16372-stderr---supervisor-SHMEi3.log
-rw------- 1 root   root   4967 May  2 18:19 redis_16372-stdout---supervisor-fvKDa8.log
-rw------- 1 root   root      0 May  2 18:14 redis_16373-stderr---supervisor-ncUWuE.log
-rw------- 1 root   root   2326 May  2 18:14 redis_16373-stdout---supervisor-ZxiqIj.log

supervisor服务

1
2
3
4
5
6
7
8
9
10
# 调整下启动脚本 start方法 中 --pidfile 的位置
[root@cu2 ~]# ll /etc/init.d/supervisord 
-rwxr-xr-x 1 root root 2977 May  2 19:29 /etc/init.d/supervisord
[root@cu2 ~]# grep daemon /etc/init.d/supervisord 
    daemon $supervisord --pidfile=${pidfile} $OPTIONS
[root@cu2 ~]# ll /etc/sysconfig/supervisord 
-rw-r--r-- 1 root root 723 May  2 19:17 /etc/sysconfig/supervisord

[root@cu2 ~]# service supervisord status
supervisord (pid  39549) is running...

参考

阿里云机器上安装supervisor - 2017-8-14 10:14:59

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
[root@iZ9416vn227Z opt]# easy_install supervisor
Searching for supervisor
Reading http://mirrors.aliyun.com/pypi/simple/supervisor/
Best match: supervisor 3.3.3
Downloading http://mirrors.aliyun.com/pypi/packages/31/7e/788fc6566211e77c395ea272058eb71299c65cc5e55b6214d479c6c2ec9a/supervisor-3.3.3.tar.gz#md5=0fe86dfec4e5c5d98324d24c4cf944bd
Processing supervisor-3.3.3.tar.gz
Running supervisor-3.3.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-vHf1qL/supervisor-3.3.3/egg-dist-tmp-eIOT5x
warning: no previously-included files matching '*' found under directory 'docs/.build'
Adding supervisor 3.3.3 to easy-install.pth file
Installing echo_supervisord_conf script to /usr/bin
Installing pidproxy script to /usr/bin
Installing supervisorctl script to /usr/bin
Installing supervisord script to /usr/bin

Installed /usr/lib/python2.6/site-packages/supervisor-3.3.3-py2.6.egg
Processing dependencies for supervisor
Searching for meld3>=0.6.5
Reading http://mirrors.aliyun.com/pypi/simple/meld3/
Best match: meld3 1.0.2
Downloading http://mirrors.aliyun.com/pypi/packages/45/a0/317c6422b26c12fe0161e936fc35f36552069ba8e6f7ecbd99bbffe32a5f/meld3-1.0.2.tar.gz#md5=3ccc78cd79cffd63a751ad7684c02c91
Processing meld3-1.0.2.tar.gz
Running meld3-1.0.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-aIaH9E/meld3-1.0.2/egg-dist-tmp-iTyXOd
zip_safe flag not set; analyzing archive contents...
Adding meld3 1.0.2 to easy-install.pth file

Installed /usr/lib/python2.6/site-packages/meld3-1.0.2-py2.6.egg
Finished processing dependencies for supervisor

其他的安装方式参考官网文章:

生成配置文件: http://supervisord.org/installing.html#creating-a-configuration-file

you can place it in the current directory (echo_supervisord_conf > supervisord.conf) and start supervisord with the -c flag in order to specify the configuration file location. For example, supervisord -c supervisord.conf

1
2
3
4
5
6
7
8
9
[root@iZ9416vn227Z opt]# echo_supervisord_conf > /etc/supervisord.conf

[root@iZ9416vn227Z opt]# vi /etc/supervisord.conf 
inet_http_server

supervisord log/pid...

[include]
files = /etc/supervisor.d/*.ini

已服务的方式启动(centos6): http://supervisord.org/running.html#running-supervisord-automatically-on-startup

1
2
3
4
5
6
7
[root@iZ9416vn227Z opt]# wget https://raw.githubusercontent.com/Supervisor/initscripts/master/redhat-init-jkoppe -O /etc/init.d/supervisord 
[root@iZ9416vn227Z opt]# chmod +x /etc/init.d/supervisord 

[root@iZ9416vn227Z opt]# wget https://raw.githubusercontent.com/Supervisor/initscripts/master/redhat-sysconfig-jkoppe -O /etc/sysconfig/supervisord 

对比sshd的启动脚本,注释掉这句后,正常输出。不知道 env -i 什么时刻加的
#set -o nounset

编写配置管理服务:

1
2
3
4
5
6
7
8
[root@iZ9416vn227Z opt]# cat /etc/supervisor.d/tomcat-test.ini 
[program:tomcat-test]
command=/opt/apache-tomcat-8.0.26-test/bin/catalina.sh run
environment=JAVA_OPTS="-Xmx512m",JAVA_HOME=/usr/java/jdk1.8.0_92
directory=/opt/apache-tomcat-8.0.26-test
autostart=true
autorestart=true
# user=root 需要tomcat目录相关的权限,谨慎

command 字段设置的是后台守护应用的启动命令, 注意: 该命令必须是在前台执行的, 即会独占控制台, 否则会导致 supervisor 无法获得标准输出, 并失去进程的控制权.

1
2
3
4
5
supervisorctl status all
supervisorctl update
supervisorctl start tomcat-test

service supervisord status

–END

Comments