使用supervisor管理python web服务
概述
supervisor就是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。
编写的Python web 程序在centos6.5上可以通过python3 app.py执行,但是退出ssh登陆之后,进程也随之结束。通过安装并配置supervisor就可以把命令行程序变成“deamon”随系统自动运行。
supervisor VS. rc.d scripts
rc.d scripts实现了基本的进程初始化、重启、管理特性,但rc.d scripts不好维护和管理。supervisor可以实现rc.d scripts同样的功能,并且能在进程崩溃时候自动重启该进程。
安装supervisor
在centos 6.5 环境下使用下面命令安装supervisor,并且使用yum info 命令看到安装的supervisor版本为2.1。
#安装 supervisor
yum install supervisor
#查看安装信息
yum info supervisor
Loaded plugins: security
Installed Packages
Name : supervisor
Arch : noarch
Version : 2.1
Release : 9.el6
Size : 1.1 M
Repo : installed
From repo : epel
Summary : A System for Allowing the Control of Process State on UNIX
URL : http://www.plope.com/software/supervisor2/
License : ZPLv2.1 and BSD and MIT
Description : The supervisor is a client/server system that allows its users to control a
: number of processes on UNIX-like operating systems.
配置supervisor
在本环境下supervisor的配置文件为/etc/supervisord.conf,其它环境可能不一样。直接通过vim编辑该文件,并在文件末尾添加上下面配置即可:
[program:awesome] command = /srv/awesome/www/app.py directory = /srv/awesome/www user = root startsecs = 3 redirect_stderr = true stdout_logfile_maxbytes = 50MB stdout_logfile_backups = 10 stdout_logfile = /srv/awesome/log/app.log
启动supervisor
在使用输入supervisorctl reload 或者 supervisorctl 前,必须先用service supervisord start 命令启动服务,否则会提示下面错误:
[root@localhost etc]# supervisorctl reload error: <class socket.error>, [Errno 2] No such file or directory: file: <string> line: 1 [root@localhsot etc]# supervisorctl error: <class socket.error>, [Errno 2] No such file or directory: file: <string> line: 1 [root@localhsot etc]# service supervisord start Starting supervisord: [ OK ] [root@localhost etc]# supervisorctl awesome RUNNING pid 28877, uptime 0:00:38
