讲过一篇celery的,但是celery启动后并不是daemon的,在生产环境中这肯定是不可以的,那怎么办呢? 这就需要使用supervisor进行进程管理了,下面详细介绍。
一、 supervisor是干什么的。
supervisor是有Python语言编写的,基于linux操作系统的一款服务器管理工具,用以监控服务器进程的运行。
supervisor要求管理的程序是非daemon程序,supervisord会帮你把他转换成daemon程序。
因此如果使用supervisor来管理nginx进程的话,必须在nginx的配置文件里添加一行设置deamon off让nginx以非daemon方式启动,
我们这里是用supervisor是用它来管理celery进程。
二、supervisor安装
1. 安装好python环境,这个Linux系统都默认已经安装好了,但是建议安装2.7.0以上版本。
2. 使用pip或者easy_install安装
sudo pip install supervisor 或者
sudo easy_install supervisor
安装完以后,进入python交互式环境,输入:
import supervisor
如果没有错误提示则表示安装成功。
三、 supervisor配置
1. superisor配置生成
$ echo_supervidord_conf > /etc/supervisord.conf
2. 配置文件说明
$ sudo cp /etc/supervisord.conf /etc/suprtvidord.conf.bak # 首先备份一下配置文件
废话不多说,直接贴配置文件(配置文件中以;开头的行表示注释):
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
; - Shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".
; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
# [unix_http_server] 这个模块主要是配置sock文件的路径及权限
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; socket file mode (default 0700)
chown=root:root ; socket file uid:gid owner
;username=admin ; (default is no username (open server))
;password=admin ; (default is no password (open server))
# [inet_http_server] 这个模块只要是配置Web管理界面
[inet_http_server] ; inet (TCP) server disabled by default
port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
username=admin ; (default is no username (open server))
password=admin ; (default is no password (open server))
# [supervisord] 这个模块是supervisord服务的配置模块,比如日志文件位置,日志等级等等
[supervisord]
logfile=/home/liuyadong/log/supervisor/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)
nodaemon=false ; (start in foreground 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=chrism ; (default is current user, required 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)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
# [supervisorctl] 这个模块的值必须和"unix_http_server"里面设定的匹配
[supervisorctl]
serverurl=unix:///var/run/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=admin ; should be same as http_username if set
;password=admin ; should be same as http_password if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The below sample program section shows all possible program subsection values,
; create one or more ‘real‘ program: sections to be able to control them under
; supervisor.
# [program:xxx] 这个模块就是需要管理的进程模块, 字段属性也很简单,后面有注释就不详细介绍了。
# 这个模块可以是多个
[program:celery]
command=celery worker -A monitorAuto.celery --loglevel=info ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
numprocs=1 ; number of processes copies to start (def 1)
directory=/home/liuyadong/git/monitor_auto/ ; directory to cwd to before exec (def no cwd)
autostart=true ; start at supervisord start (default: true)
startsecs=10 ; # of secs prog must stay up to be running (def. 1)
startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=ture ; 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)
stopwaitsecs=600 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
killasgroup=true ; SIGKILL the UNIX process group (def false)
user=liuyadong ; setuid to this UNIX account to run the program
redirect_stderr=true ; redirect proc stderr to stdout (default false)
stdout_logfile=/home/liuyadong/log/supervisor/supercelery.log ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB ; number of bytes in ‘capturemode‘ (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
stderr_logfile=/home/liuyadong/log/supervisor/supercelery.error.log ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in ‘capturemode‘ (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A="1",B="2" ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more ‘real‘
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.
;[group:thegroupname]
;programs=progname1,progname2 ; each refers to ‘x‘ in [program:x] definitions
;priority=999 ; the relative start priority (default 999)
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
;[include]
;files = relative/directory/*.ini
四、 Supervisord管理 Supervidord安装完成以后有两个可用的命令行supervisor和supervisorctl
? supervisord, 初始启动Supervisord,启动、管理配置中设置的进程。
? supervisorctl stop programxxx 停止某一个进程(programxxx),programxxx为[program:chatdemon]里配置的值,这个示例就是chatdemon。
? supervisorctl start programxxx 启动某个进程
? supervisorctl restart programxxx 重启某个进程
? supervisorctl stop groupworker 重启所有属于名为groupworker这个分组的进程(start,restart同理)
? supervisorctl stop all 停止全部进程,注:start、restart、stop都不会载入最新的配置文件。
? supervisorctl reload 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程。
? supervisorctl update 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。
注意:显示用stop停止掉的进程,用reload或者update都不会自动重启。
可以使用--help查看命令更多的用法,我启动时遇到配置文件没有放到默认路径使用-c 参数执行
时间: 2024-10-25 22:35:27