[Linux系统] 使用supervisor管理进程

一、安装supervisor

由于supervisor暂不支持python3,所以我们使用python2的easy_install安装:

[root@centos-venv-fb ~]# python -V
Python 2.7.5
[root@centos-venv-fb ~]# easy_install -i https://pypi.tuna.tsinghua.edu.cn/simple supervisor
Searching for supervisor
Reading https://pypi.tuna.tsinghua.edu.cn/simple/supervisor/
Best match: supervisor 4.1.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/de/87/ee1ad8fa533a4b5f2c7623f4a2b585d3c1947af7bed8e65bc7772274320e/supervisor-4.1.0.tar.gz#sha256=2dc86fe0476e945e61483d614ceb2cf4f93b95282eb243bdf792621994360383
Processing supervisor-4.1.0.tar.gz
Writing /tmp/easy_install-tUwPwD/supervisor-4.1.0/setup.cfg
Running supervisor-4.1.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-tUwPwD/supervisor-4.1.0/egg-dist-tmp-92tQX5
warning: no previously-included files matching '*' found under directory 'docs/.build'
Adding supervisor 4.1.0 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.7/site-packages/supervisor-4.1.0-py2.7.egg
Processing dependencies for supervisor
Finished processing dependencies for supervisor

二、配置supervisor

1.生成supervisord.conf

[root@centos-venv-fb ~]# echo_supervisord_conf > /etc/supervisord.conf
[root@centos-venv-fb etc]# ll /etc/super*
-rw-r--r--. 1 root root 10535 Jan 31 03:45 /etc/supervisord.conf

2.修改配置文件之前准备工作

查看uwsgi的绝对路径:

# 进入虚拟环境,查看uwsgi绝对路径
[root@centos-venv-fb ~]# workon venv_fb
(venv_fb) [root@centos-venv-fb ~]# which uwsgi
/root/.virtualenvs/venv_fb/bin/uwsgi

再找到uwsgi需要的ini配置文件的绝对路径:

/root/admin_demo/uwsgi.ini

3.修改配置文件

vi /etc/supervisord.conf

在配置文件的最后添加以下内容:

[program:admin_demo]
command=/root/.virtualenvs/venv_fb/bin/uwsgi --ini /root/admin_demo/uwsgi.ini

这条命令是使用uwsgi运行django项目。

其他配置项:

[program:admin_demo]
command=/root/.virtualenvs/venv_fb/bin/uwsgi --ini /root/admin_demo/uwsgi.ini

autostart = true     ; 在 supervisord 启动的时候也自动启动
autorestart = true   ; 程序异常退出后自动重启
startsecs = 5        ; 启动 5 秒后没有异常退出,就当作已经正常启动了
startretries = 3     ; 启动失败自动重试次数,默认是 3
redirect_stderr = true  ; 把 stderr 重定向到 stdout,默认 false
user=root                
loglevel=info

三、使用supervisor

1.启动supervisord

执行命令启动supervisord进程:

[root@centos-venv-fb etc]# supervisord -c /etc/supervisord.conf
Unlinking stale socket /tmp/supervisor.sock
[root@centos-venv-fb etc]# ps -ef | grep super                 
root       4056      1  0 04:03 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root       4070   3565  0 04:03 pts/0    00:00:00 grep --color=auto super

启动program:admin_demo:

[root@centos-venv-fb etc]# supervisorctl -c /etc/supervisord.conf 
admin_demo                       RUNNING   pid 4504, uptime 0:00:08

此时查看uwsgi进程:

[root@centos-venv-fb supervisor]# ps -ef |grep uwsgi
root       4504   4503  0 04:48 ?        00:00:00 /root/.virtualenvs/venv_fb/bin/uwsgi --ini /root/admin_demo/uwsgi.ini
root       4505   4504  0 04:48 ?        00:00:00 /root/.virtualenvs/venv_fb/bin/uwsgi --ini /root/admin_demo/uwsgi.ini
root       4506   4504  0 04:48 ?        00:00:00 /root/.virtualenvs/venv_fb/bin/uwsgi --ini /root/admin_demo/uwsgi.ini
root       4507   4504  0 04:48 ?        00:00:00 /root/.virtualenvs/venv_fb/bin/uwsgi --ini /root/admin_demo/uwsgi.ini
root       4508   4504  0 04:48 ?        00:00:00 /root/.virtualenvs/venv_fb/bin/uwsgi --ini /root/admin_demo/uwsgi.ini
root       4509   4504  0 04:48 ?        00:00:00 /root/.virtualenvs/venv_fb/bin/uwsgi --ini /root/admin_demo/uwsgi.ini
root       4520   3958  0 04:52 pts/1    00:00:00 grep --color=auto uwsgi

2.停止进程(踩坑)

[root@centos-venv-fb etc]# supervisorctl -c /etc/supervisord.conf 
admin_demo                       RUNNING   pid 4504, uptime 0:03:41
supervisor> stop admin_demo  # 或者stop all,结束所管理的所有进程
admin_demo: stopped

此时查看uwsgi进程:

[root@centos-venv-fb supervisor]# ps -ef |grep uwsgi
root       4526      1 49 04:55 ?        00:00:08 /root/.virtualenvs/venv_fb/bin/uwsgi --ini /root/admin_demo/uwsgi.ini
root       4538   3958  0 04:55 pts/1    00:00:00 grep --color=auto uwsgi

我们看到,虽然我们使用了stop admin_demo,但是uwsgi遗留了两条进程,这两条进程编程了孤儿进程(父进程是init gid=1)。

????具体原因不明。。可能是因为uwsgi的实现问题。其他程序可能不存在这种问题。(坑)

3.其他命令

supervisorctl start program_name  # 启动某一进程
supervisorctl stop program_name  # 停止某一进程
supervisorctl restart program_name  # 重启某一进程
supervisorctl reload  # 重启启动配置中的所有程序
supervisorctl stop all  # 停止所有程序
supervisorctl update  # 更新配置

 

≧▂≦ 

你可能感兴趣的:([Linux系统] 使用supervisor管理进程)