背景知识:
supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。
它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动。
怎么用?
只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。
安装:
sudo apt-get install supervisor
配置文件默认位置:/etc/supervisor/supervisord.conf
实践一下:
步骤1 :安装:pip3 install supervisor
步骤2: 生成配置文件:echo_supervisord_conf > /etc/supervisord.conf
步骤3: 编辑配置文件:/etc/supervisord.conf 添加自己需要的服务
如下:
[unix_http_server]
file=/tmp/supervisor.sock ; the path to the socket file
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
[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]
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
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[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:cert108]
directory=/Users/koutouyuan/Desktop/flask_server
command=python3 server.pyc ;the program (relative uses PATH, can take args)
process_name=flask_server ; process_name expr (default %(program_name)s)
autostart=true ; start at supervisord start (default: true)
autorestart=true
startsecs=1 ; of secs prog must stay up to be running (def. 1)
startretries=3 ; max # of serial start failures when starting (default 3)
autorestart=unexpected ; when to restart if exited after running (def: unexpected)
exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2)
stopsignal=QUIT ; signal used to kill process (default TERM)
stdout_logfile=NONE ; stdout log path, NONE for none; default AUTO
stderr_logfile=NONE ; stderr log path, NONE for none; default AUTO
步骤4: sudo supervisord -c supervisord.conf 启动supervisor服务
最后,通过 http://127.0.0.1:9001查看运行情况;
更多管理命令:
- supervisorctl status 查看进程运行状态
- supervisorctl start 进程名 启动进程
- supervisorctl stop 进程名 关闭进程
- supervisorctl restart 进程名 重启进程
- supervisorctl update 重新载入配置文件
- supervisorctl shutdown 关闭supervisord
- supervisorctl clear 进程名 清空进程日志
- supervisorctl 进入到交互模式下。使用help查看所有命令。
- start stop restart + all 表示启动,关闭,重启所有进程。