如何使用Supervisor监控和管理python进程
1 安装
wget https://bootstrap.pypa.io/get-pip.py
python2 get-pip.py
pip2 install supervisor
echo_supervisord_conf >/etc/supervisord.conf
安装过程假如遇到如下问题:
pip2 install supervisor
出现:error: invalid command 'bdist_wheel' Failed building wheel for supervisorecho_supervisord_conf
出现:pkg_resources.DistributionNotFound: meld3>=0.6.5
这是由于pip和setuptools的兼容性问题所致。 升级setuptool模块:
pip2 install setuptools --upgrade
如果没有root权限还想装,可以参考使用 pyenv 管理 Python 版本
$ pyenv install -l # 可用版本
$ pyenv version -l # 当前版本
$ pyenv install pypy2.7-5.9.0
$ pyenv versions
system
* 3.6.3 (set by /home/work/.pyenv/version)
pypy2.7-5.9.0
$ pyenv virtualenv pypy2.7-5.9.0 venv27
$ ls ~/.pyenv/versions/
3.6.3/ pypy2.7-5.9.0/ venv27/
$ . venv27/bin/activate
$ python --version
Python 2.7.13 (84a2f3e6a7f88f2fe698e473998755b3bd1a12e2, Oct 03 2017, 16:11:46)
[PyPy 5.9.0 with GCC 7.1.0]
$ pip install virtualenv
2 配置文件
vim /etc/supervisord.conf
; ref http://shouce.jb51.net/introduction-to-tornado/ch8.html
[group:tornadoes] ;声明了一个叫作tornadoes的组
programs=tornado-8000,tornado-8001,tornado-8002
[program:tornado-8000]
command=python /var/www/main.py --port=8000 ; 注意后台程序的端口
directory=/var/www ;程序的工作目录
user=www-data
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/tornado.log ;日志文件
loglevel=info
[program:tornado-8001]
command=python /var/www/main.py --port=8001
directory=/var/www
user=www-data
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/tornado.log
loglevel=info
[program:tornado-8002]
command=python /var/www/main.py --port=8002
directory=/var/www
user=www-data
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/tornado.log
loglevel=info
配置详解:http://www.cnblogs.com/ajianbeyourself/p/5534737.html
3 启动
# supervisord
/usr/lib/python2.6/site-packages/supervisor/options.py:298: 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 '
# supervisorctl
tornadoes:tornado-8000 RUNNING pid 32091, uptime 00:00:02
tornadoes:tornado-8001 RUNNING pid 32092, uptime 00:00:02
tornadoes:tornado-8002 RUNNING pid 32093, uptime 00:00:02
# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
├─agetty
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon
├─irqbalance
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─nginx───nginx
├─polkitd───5*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd─┬─sshd───sshd───bash───su───bash
│ └─sshd───sshd───bash───su───bash───pstree
├─2*[supervisord]
├─supervisord───3*[python3]
├─systemd-journal
├─systemd-logind
├─systemd-udevd
└─tuned───4*[{tuned}]
4 开机自动运行
curl http://git.oschina.net/snifferapache/codes/gxmvtfe1pnl5w4hbdc36y97/raw > /etc/init.d/supervisord
chmod +x /etc/init.d/supervisord
# in debian based:
sudo update-rc.d supervisord defaults
# in redhat
chkconfig --add supervisord
设置开机启动:chkconfig supervisord on
查看是否成功:chkconfig --list | grep supervisord
5 修改配置,重启
:reload your_program
reload重新载入supervisord.conf,一般只在修改supervisord.conf后使用,这个会让修改相关程序配置的程序关闭。没有修改过的程序配置的程序不会有影响。
作者:kakashis
联系方式:fengshenjiev[AT]gmail.com
本文版权归作者所有,欢迎转载,演绎或用于商业目的,但是必须说明本文出处(包含链接)。