[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)
[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
[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] ifset ;password=123 ; should be same as in [*_http_server] ifset ;prompt=mysupervisor ; cmd line prompt (default "supervisor") ;history_file=~/.sc_history ; use readline historyif available
;[program:theprogramname] ;command=/bin/cat ; the program (relative uses PATH, can take args)
;[group:thegroupname] ;programs=progname1,progname2 ; each refers to 'x'in [program:x] definitions ;priority=999 ; the relative start priority (default 999)
supervisor> reload Really restart the remote supervisord process y/N? y Restarted supervisord supervisor> status test1 RUNNING pid 3253, uptime 0:00:02 test2 STOPPED Not started
default commands (typehelp): ===================================== add exit open reload restart start tail avail fg pid remove shutdown status update clear maintail quit reread signal stop version
通过
help cmd 可以查看每个命令的意义和用法:
supervisor> help restart restart Restart a process restart :* Restart all processes in a group restart Restart multiple processes or groups restart all Restart all processes Note: restart does not reread config files. For that, see reread and update.
其中与 supervisord 服务进程相关的命令有:
open : 连接到远程 supervisord 进程。
reload : 重启 supervisord 进程。
shutdown : 关闭 supervisord 进程。
而以下命令则用于进行具体的应用进程管理:
status : 查看应用进程的运行状态。
start : 启动指定的应用进程。
restart : 重启指定的应用进程。
stop : 停止指定的应用进程。
signal : 向指定应用进程发送信号。
update : 重新加载配置参数,并根据需要重启应用进程。
5.4. 应用进程的信号处理
在某些应用场景,需要在进程结束前进行一些处理操作,比如清理缓存,上传执行状态等。对于这种需求可以通过引入 signal 模块并注册相关处理逻辑,同时结合 supervisorctl
的 signal 命令来实现。
测试代码如下:
import time import signal
# 运行标志 RUN = True
# 信号处理逻辑 def exit_handler(signum, frame): print(f'processing signal({signal.Signals(signum).name})') print("update task status") print("clear cache data") global RUN RUN = False
if self.config.options.mood > SupervisorStates.RESTARTING: # dont start any processes if supervisor is shutting down if state == ProcessStates.EXITED: if self.config.autorestart: if self.config.autorestart is RestartUnconditionally: # EXITED -> STARTING self.spawn() else: # autorestart is RestartWhenExitUnexpected if self.exitstatus not in self.config.exitcodes: # EXITED -> STARTING self.spawn() elif state == ProcessStates.STOPPED and not self.laststart: if self.config.autostart: # STOPPED -> STARTING self.spawn() elif state == ProcessStates.BACKOFF: if self.backoff <= self.config.startretries: if now > self.delay: # BACKOFF -> STARTING self.spawn()
import time print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))))
supervisor> status test BACKOFF Exited too quickly (process log may have details) supervisor> status test STARTING supervisor> status test FATAL Exited too quickly (process log may have details)
import time print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time())))) time.sleep(3)
supervisor> status test RUNNING pid 7049, uptime 0:00:02 supervisor> status test EXITED Apr 03 09:41 AM