supervisor 是一款运行在类 unix 系统上的进程管理器,因此只能安装在 linux 或 macOS 系统上。
更多详情查看官方 文档
安装pip 方式安装
linux 或 MacOS 系统自带 python 安装环境,因此可以直接使用 pip 命令进行安装,如果提示命令不存在,需要先安装 python
pip install supervisor
yum 或 apt 方式安装
yum
# 看安装源中的版本 yum info supoervisor # 安装 yum install -y supervisor
apt 方式安装
# 查看安装源中的版本 apt-cache show supervisor # 安装 apt-get install supervisor
安装完成后会添加几个系统命令 supervisord supervisorctl echo_supervisord_conf
- supervisord 进监监管者
- supervisorctl 进程管理者
- echo_supervisord_conf 示例配置文件
安装完成后,会自带一个配置文件示例,运行 echo_supervisord_conf 会将配置文件示例内容输出到控制台。
生成配置文件
这里我选择将配置文件放在 /etc/supervisor 目录下。
echo_supervisord_conf > /etc/supervisor/supervisord.conf
编辑配置文件
示例配置文件中 file logfile pidfile 的路径都是在 /tmp 目录下, /tmp 为系统系统缓存目录过一天就会删除,因此需要修改一个路径。这里将 /tmp 修改为 /var/run 。
vim /etc/supervisor/supervisord.conf # 第一处修改 [unix_http_server] file=/var/run/supervisor.sock ; (the path to the socket file) # 第二处修改 [supervisord] logfile=/var/run/supervisord.log ; (main log file;default $CWD/supervisord.log) logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) logfile_backups=10 ; (num of main logfile rotation backups;default 10) loglevel=info ; (log level;default info; others: debug,warn,trace) pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) # 第三处修改 [supervisorctl] serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
将配置文件最后的两行的注释打开并修改为如下内容
# 新建 conf.d 目录 mkdir -p /etc/supervisor/conf.d # 将配置文件最后修改如下 [include] files = /etc/supervisor/conf.d/*.conf
添加单个进程配置文件
在 conf.d 目录中新建 test.conf 文件,示例配置如下
[program:test] process_name=%(program_name)s; 如果是多个进程必须设置这个,如果只启动一个进程这行配置可以没有 command=/usr/local/php/bin/php /www/swoole.php autostart=true autorestart=true ; 是否自动重启 user=root ; 执行用户 numprocs=4 ; 启动的进程数 redirect_stderr=true stdout_logfile=/var/www/test/log/test.log ; 输出日志文件路径
更多的配置选项可以参考配置文件中 [program:theprogramname] 下的相关内容
启动启动主进程
supervisord -c /etc/supervisor/supervisord.conf
管理进程
# 重载配置文件,每次添加或修改配置文件时需要重新载入 supervisorctl reload # 停止某个进程 supervisorctl stop 进程名 # 停止所进程 supervisorctl stop all # 启动某个进程 supervisorctl start 进程名 # 启动所有进程 supervisorctl start all设置开机启动
vim /etc/rc.local # 在最后一行添加 /usr/bin/supervisord -c /etc/supervisor/supervisord.conf



