Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supervisor 安装使用 #27

Open
johnnian opened this issue Aug 10, 2017 · 0 comments
Open

Supervisor 安装使用 #27

johnnian opened this issue Aug 10, 2017 · 0 comments

Comments

@johnnian
Copy link
Owner

johnnian commented Aug 10, 2017

之前在生产环境部署Web服务,使用 nohup来启动服务,但是nohup在异常崩溃的情况下,无法重启服务,找到 Supervisor , 可以实现后台守护进程的方式运行服务。

一、介绍

Supervisor是比较常用的进程管理工具,支持 Linux/MacOS平台,可以用来控制一组Linux/Unix进程(启动、重启、kill)等,使用Supervisor管理的进程,可以做到以守护进程的方式运行,服务异常关闭后可以自动重启。

Supervisor 有下面几个组件:

  • supervisord: Supervisor的服务端程序,使用前,需要先启动该组件;
  • supervisorctl: Supervisor的客户端程序,用来实际控制子进程(自定义的服务、程序);

Supervisor 通过配置文件,还可以启动Web控制台,通过Web页面来管理子进程;

二、安装

环境:
CentOS : 6/7
Python : 2.X

安装方法1:使用Python的Setuptools 软件包来安装:

[root@4fff02d62bba ~]# yum -y install python-setuptools
[root@4fff02d62bba ~]# easy_install supervisor
[root@4fff02d62bba ~]# supervisord -v
3.3.3

如果需要使用非ROOT用户安装,则参考:

export PYTHONPATH=$PYTHONPATH:/home/yoursiteuser/bin
easy_install --install-dir=/home/yoursiteuser/bin supervisor

安装方法2:使用yum安装

yum install epel-release
yum install -y supervisor
systemctl enable supervisord # 开机自启动
systemctl start supervisord # 启动supervisord服务

三、配置 & 启动服务

拷贝默认配置文件

[root@4fff02d62bba ~]# mkdir /etc/supervisor
[root@4fff02d62bba ~]# echo_supervisord_conf > /etc/supervisord.conf
[root@4fff02d62bba ~]# vi /etc/supervisord.conf
;修改下面路径
[unix_http_server]
file=/etc/supervisor/supervisor.sock   ; the path to the socket file

;修改下面路径
[supervisord]
logfile=/etc/supervisor/supervisord.log ; main log file; default $CWD/supervisord.log
pidfile=/etc/supervisor/supervisord.pid ; supervisord pidfile; default supervisord.pid

;修改下面路径
[supervisorctl]
serverurl=unix:///etc/supervisor/supervisor.sock ; use a unix:// URL  for a unix socket

;修改include配置,去除注释
[include]
files = /etc/supervisor/*.conf

[root@4fff02d62bba ~]# supervisord -c /etc/supervisord.conf
[root@4fff02d62bba ~]# ps -ef | grep supervisord
root       202     0  0 03:24 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf

四、配置应用或服务器进程

编写应用启动脚本:启动脚本还有其他的选项配置,点击查看官网文档

[root@4fff02d62bba ~]# vi /etc/supervisor/storm.conf
[program:storm]
directory=/root
command=storm supervisor
autostart=true
autorestart=true

配置文件立即生效:

#(修改的配置文件生效,设置`autostart=true`的程序,会自动启动)
[root@4fff02d62bba ~]# supervisorctl update
Restarted supervisord

启动或关闭应用:

#启动全部应用
[root@4fff02d62bba ~]# supervisorctl start all
#关闭全部应用
[root@4fff02d62bba ~]# supervisorctl stop all
#重启全部应用
[root@4fff02d62bba ~]# supervisorctl restart all

启动多个同一进程:

PS: 同一个应用程序,启动多个进程,默认情况下,这些进程都归属于同一个组别,例如下面,test应用启动了2个进程,则这两个进程就归属于 test

[root@ 4fff02d62bba ~]# vi /etc/supervisor/test.conf
[program:test]
directory=/root
command=/root/test
numprocs=2
process_name=%(program_name)s_%(process_num)s
autostart=true
autorestart=true

#查看后台进程:
[root@ 4fff02d62bba ~]# supervisorctl status
test:test_0                        RUNNING   pid 81, uptime 0:00:02
test:test_1                        RUNNING   pid 80, uptime 0:00:02

启动或者关闭一组进程:

[root@ 4fff02d62bba ~]# vi /etc/supervisor/test.conf
[group:foo]
programs=bar,baz
priority=999

#启动组别进程的命令
[root@75d336b8c2cf log]# supervisorctl help start
start <name>		Start a process
start <gname>:*		Start all processes in a group
start <name> <name>	Start multiple processes or groups
start all		Start all processes

[root@75d336b8c2cf log]# supervisorctl start foo:
fazoo:bar: started
foo:b: started

#关闭组别进程的命令
[root@75d336b8c2cf log]# supervisorctl help stop
stop <name>		Stop a process
stop <gname>:*		Stop all processes in a group
stop <name> <name>	Stop multiple processes or groups
stop all		Stop all processes

[root@75d336b8c2cf ~]# supervisorctl stop foo:
fazoo:bar: stopped
foo:b: stopped

以其他用户角色启动进程:

[root@ 4fff02d62bba ~]# vi /etc/supervisor/test.conf
[program:test]
user=test
...

备注:

  • supervisorctl 还有其他命令,点击这里查看官网文档
  • supervisorctl 命令的默认配置文件是:/etc/supervisord.conf
  • 除了上述的配置外,还可以设置输出的日志信息等,具体,可以参考官网。

五、常见问题

1、使用普通用户控制supervisor

假设普通用户名为:testuser

  • 步骤1:使用root用户安装supervisor
  • 步骤2:创建普通用户组
groupadd supervisor
usermod -a testuser -G supervisor
  • 步骤3:修改以下配置文件
[testuser@4fff02d62bba ~]# vi /path/to/supervisord.conf
;修改下面路径
[unix_http_server]
file=/etc/supervisor/supervisor.sock   ; the path to the socket file
chmod=0766                 ; socket file mode (default 0700)
chown= testuser:supervisor     ; socket file uid:gid owner

删除默认路径下的: /etc/supervisord.conf

  • 步骤4:使用普通用户启动supervisord
supervisord -c  /path/to/supervisord.conf

参考链接

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant