简介
**supervisor的作用**
>* 方便快捷的启动、重启、关闭进程
>* 服务挂掉后,自动重启
>* 可执行文件或者配置文件修改后,服务自动重启
一、安装
1.通过pip或者brew安装
- 有两种安装方式,一种是通过brew安装
brew install supervisor
- 另一种是通过pip安装
pip install supervisor
如果安装过程中提示权限问题,直接加上`sudo`进行安装
2.检查是否安装成功
- pip查看
代码语言:javascript复制pip show supervisor
Name: supervisor
Version: 4.2.1
Summary: A system for controlling process state under UNIX
Home-page: http://supervisord.org/
Author: Chris McDonough
Author-email: chrism@plope.com
License: BSD-derived (http://www.repoze.org/LICENSE.txt)
Location: /Users/username/.pyenv/versions/3.7.5/lib/python3.7/site-packages
Requires:
Required-by:
- brew查看
代码语言:javascript复制brew info supervisor
supervisor: stable 4.2.0 (bottled)
Process Control System
http://supervisord.org/
Not installed
From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/supervisor.rb
==> Dependencies
Required: python@3.8 ✔
==> Caveats
To have launchd start supervisor now and restart at login:
brew services start supervisor
Or, if you don't want/need a background service you can just run:
supervisord -c /usr/local/etc/supervisord.ini
==> Analytics
install: 1,350 (30 days), 3,573 (90 days), 13,475 (365 days)
install-on-request: 1,335 (30 days), 3,518 (90 days), 13,131 (365 days)
build-error: 0 (30 days)
二、配置文件
以brew安装为例,从上面的查找的信息中找到配置文件的位置:`/usr/local/etc/supervisord.ini`
1.进入文件夹后,发现并没有supervisord.ini文件,那我们手动生成一个配置文件
代码语言:javascript复制echo_supervisord_conf > /usr/local/etc/supervisord.ini
2.生成后,开始修改这个配置文件
- 打开监听TCP socket,启动inet的http服务。
>这个模块默认是未开启状态,打开后可通过web界面的方式控制子进程的状态。
代码语言:javascript复制[inet_http_server] ; inet (TCP) server disabled by default
port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface
username=user ; default is no username (open server)
password=123 ; default is no password (open server)
- supervisord进程全局配置
>相当于supervisor的服务端,需要配置日志文件等
代码语言:javascript复制[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
silent=false ; no logs to stdout if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
;umask=022 ; process file creation umask; default 022
;user=supervisord ; setuid to this UNIX account at startup; recommended if root
;identifier=supervisor ; supervisord identifier, default is 'supervisor'
;directory=/tmp ; default is not to cd during start
;nocleanup=true ; don't clean up tempfiles at start; default false
;childlogdir=/tmp ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value" ; key value pairs to add to environment
;strip_ansi=false ; strip ansi escape codes in logs; def. false
- supervisorctl配置
>supervisorctl相当于客户端,`serverurl`即为supervisord的服务地址,默认配置是:`http://localhost:9001`
代码语言:javascript复制[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris ; should be same as in [*_http_server] if set
;password=123 ; should be same as in [*_http_server] if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
- program
>需要用来监控的子进程配置, `program:`:设置子进程的名称,也即web界面中展示的进程名称 `command=`:为启动子进程的命令行 `directory`:为执行的项目目录 `stdout_logfile`:log日志输出文件地址
代码语言:javascript复制[program:appium]
command=python /Users/pc/Program/androidtest/manage.py runserver 0.0.0.0:9000 ; the program (relative uses PATH, can take args)
directory=/Users/pc/Program/androidtest ; directory to cwd to before exec (def no cwd)
autostart=true ; start at supervisord start (default: true)
startsecs=5 ; # of secs prog must stay up to be running (def. 1)
startretries=3 ; max # of serial start failures when starting (default 3)
stdout_logfile=/Users/pc/Program/androidtest/supervisord.log ; stdout log path, NONE for none; default AUTO
stdout_logfile_maxbytes=20MB ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10)
三、启动
1.启动supervisor
supervisord -c supervisord.ini
2.进入web后台管理页面
浏览器中输入:`http://127.0.0.1:9001`
首次进入会提示输入账号和密码,即在`inet_http_server`模块中设置的`username/password`
3.登录成功后,及可通过web界面来操作已经配置的子进程
四、常用命令
#关闭所有任务 supervisorctl shutdown # 启动某个进程 supervisorctl start programxxx # 重启某个进程 supervisorctl restart programxxx # 停止全部进程 注:start、restart、stop都不会载入最新的配置文件 supervisorctl stop all # 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程。 supervisorctl reload # 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。 supervisorctl update