安装Supervisord
由于是用python开发的,因此使用pip安装最为方便。
$ pip install supervisor
说明:安装完成之后多了3个工具:echo_supervisord_conf、supervisorctl和supervisord。
Supervisord配置文件
创建supervisor所需目录
# mkdir /etc/supervisord.d/
创建supervisor配置文件
# echo_supervisord_conf > /etc/supervisord.conf
编辑supervisord.conf文件
# vim /etc/supervisord.conf
文件内容如下
; Sample supervisor config file.
;
[unix_http_server]
file=/var/run/supervisor.sock ; UNIX socket 文件,supervisorctl 会使用
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
[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)
[supervisord]
logfile=/var/log/supervisord.log ; 日志文件,默认是 $CWD/supervisord.log
logfile_maxbytes=50MB ; 日志文件大小,超出会 rotate,默认 50MB
logfile_backups=10 ; 日志文件保留备份数量默认 10
loglevel=info ; 日志级别,默认 info,其它: debug,warn,trace
pidfile=/var/run/supervisord.pid ; supervisord pid 文件;
nodaemon=false ; 是否在前台启动,默认是 false,即以 daemon 的方式启动
minfds=1024 ; 可以打开的文件描述符的最小值,默认 1024
minprocs=200 ; 可以打开的进程数的最小值,默认 200
;umask=022 ; process file creation umask; default 022
;user=chrism ; default is current user, required if root
;identifier=supervisor ; supervisord identifier, default is 'supervisor'
;directory=/tmp ; default is not to cd during start
;nocleanup=true ; don't clean up tempfiles at start; default false
;childlogdir=/tmp ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value" ; key value pairs to add to environment
;strip_ansi=false ; strip ansi escape codes in logs; def. false
; The rpcinterface:supervisor 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:x] sections.
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
; The supervisorctl section configures how supervisorctl will connect to
; supervisord. configure it match the settings in either the unix_http_server
; or inet_http_server section.
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; 通过 UNIX socket 连接 supervisord,路径与 unix_http_server 部分的 file 一致
;serverurl=http://127.0.0.1:9001 ; 通过 HTTP 的方式连接 supervisord
;username=chris ; should be same as in [*_http_server] if set
;password=123 ; should be same as in [*_http_server] if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The sample program section below shows all possible program subsection values.
; Create one or more 'real' program: sections to be able to control them under
; supervisor.
[program:jst]
directory=/www/wwwroot/jstapi.4008737773.com ; 项目目录
command=/usr/bin/php think jstamqp ; 需要执行的命令
process_name=%(process_num)02d ; 进程命名规则
numprocs=3 ;#启动几个进程
autostart=true ;#随着supervisord的启动而启动
autorestart=true ;#自动启动
startsecs=1 ;#程序重启时候停留在runing状态的秒数
startretries=5 ;#启动失败时的最多重试次数
redirect_stderr=true ;#重定向stderr到stdout
stdout_logfile=/home/log/jstamqp.log ;#stdout文件
stdout_logfile_maxbytes=100MB ; stdout 日志文件大小,默认50MB
stdout_logfile_backups=5 ; stdout 日志文件备份数,默认是10
user=www ; 执行用户需要与php-fpm运行用户相同
[include]
files = /etc/supervisord.d/*.ini
启动supervisor
$ supervisord -c /etc/supervisord.conf
查看supervisor是否启动成功
# ps -ef|grep supervisord
root 932 1 0 May10 ? 00:00:09 /bin/python2.7 /bin/supervisord -c /etc/supervisord.conf
root 7902 6814 0 10:29 pts/0 00:00:00 grep --color=auto supervisord
将supervisor配置为开机自启动服务
编辑服务文件
$ vim /usr/lib/systemd/system/supervisord.service
内容如下
[Unit]
Description=Supervisor daemon
[Service]
Type=forking
PIDFile=/var/run/supervisord.pid
ExecStart=/bin/supervisord -c /etc/supervisord.conf
ExecStop=/bin/supervisorctl shutdown
ExecReload=/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
设置服务自启动
$ systemctl enable supervisord
查看是否启动
$ systemctl is-enabled supervisord
enabled
成功之后,就可以使用如下命令管理supervisor服务了
$ systemctl stop supervisord
$ systemctl start supervisord
$ systemctl status supervisord
$ systemctl reload supervisord
$ systemctl restart supervisord
参考链接: