Supervisor 介绍
Supervisor 是一个用 Python 写的进程管理工具,可以很方便的用来启动、重启、关闭进程(不仅仅是 Python 进程)。除了对单个进程的控制,还可以同时启动、关闭多个进程,比如很不幸的服务器由于某种原因暂时 kill 掉你的应用,此时可以用 Supervisor 让你的应用自动重启,如果是多个应用被杀死,也省去了手动一个一个地敲命令重新启动。
安装
目前 Supervisor 只能运行在 Unix-Like 的系统上,无法运行在 Windows 上。Supervisor 官方版目前只能运行在 Python 2.4 以上版本,但是还无法运行在 Python 3 上。执行下面代码前,需要安装 pip:
pip install supervisor
安装完成后,可以使用两个命令,分别是 supervisord 和 supervisorctl,如果你的系统里有两个版本的 Python,且默认的 python 命令版本是 Python 3,此时运行会出错,解决方式是修改两个命令使用的 Python 版本。使用 which 命令找到两个命令的文件地址,然后编辑文件并指定 Python 版本:
修改 Python 版本
编辑配置文件
将下面内容保存到 /etc/supervisor/supervisord.conf:
; 基础配置样例
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; ad