Supervisor 开始

Supervisor 是 Linux/Unix 操作系统上的进程管理工具。本文介绍了于 Ubuntu 18 上如何使用 Supervisor 开机启动、保活守护自己的服务进程。

安装

建议系统方式安装,可开机启动。

1sudo apt install supervisor -y

Systemd 查看服务状态:

1$ sudo systemctl status supervisor 2● supervisor.service - Supervisor process control system for UNIX 3 Loaded: loaded (/lib/systemd/system/supervisor.service; enabled; vendor preset: enabled) 4 Active: active (running) since Tue 2021-06-08 18:00:00 CST; 4min 0s ago 5 Docs: http://supervisord.org 6 Main PID: 26297 (supervisord) 7 Tasks: 1 (limit: 4915) 8 CGroup: /system.slice/supervisor.service 9 └─26297 /usr/bin/python /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf 10 116月 08 18:00:00 john-ubuntu18 systemd[1]: Started Supervisor process control system for UNIX. 126月 08 18:00:00 john-ubuntu18 supervisord[26297]: 2021-06-08 18:00:00,422 CRIT Supervisor running as root (no user in config file) 136月 08 18:00:00 john-ubuntu18 supervisord[26297]: 2021-06-08 18:00:00,422 WARN No file matches via include "/etc/supervisor/conf.d/*.conf" 146月 08 18:00:00 john-ubuntu18 supervisord[26297]: 2021-06-08 18:00:00,428 INFO RPC interface 'supervisor' initialized 156月 08 18:00:00 john-ubuntu18 supervisord[26297]: 2021-06-08 18:00:00,428 CRIT Server 'unix_http_server' running without any HTTP authentication checki 166月 08 18:00:00 john-ubuntu18 supervisord[26297]: 2021-06-08 18:00:00,428 INFO supervisord started with pid 26297
  • Loaded: 配置路径; 开机启动状态
  • Active: 活动状态
  • Main PID: 主进程 ID
  • Tasks: 任务数量
  • CGroup: 子进程树

Systemd 的服务配置:

1$ cat /lib/systemd/system/supervisor.service 2[Unit] 3Description=Supervisor process control system for UNIX 4Documentation=http://supervisord.org 5After=network.target 6 7[Service] 8ExecStart=/usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf 9ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown 10ExecReload=/usr/bin/supervisorctl -c /etc/supervisor/supervisord.conf $OPTIONS reload 11KillMode=process 12Restart=on-failure 13RestartSec=50s 14 15[Install] 16WantedBy=multi-user.target

如果 pip 安装 Supervisor,可如上添加 Systemd 服务配置:

1pip install supervisor 2 3# 添加配置 4sudo -i 5cat <<-EOF > /etc/systemd/system/supervisord.service 6[Unit] 7Description=Supervisor process control system for UNIX 8Documentation=http://supervisord.org 9After=network.target 10 11[Service] 12ExecStart=/home/john/anaconda3/bin/supervisord/supervisord -n -c /etc/supervisor/supervisord.conf 13ExecStop=/home/john/anaconda3/bin/supervisord/supervisorctl $OPTIONS shutdown 14ExecReload=/home/john/anaconda3/bin/supervisord/supervisorctl -c /etc/supervisor/supervisord.conf $OPTIONS reload 15KillMode=process 16Restart=on-failure 17RestartSec=50s 18 19[Install] 20WantedBy=multi-user.target 21EOF 22 23# 重载配置 24systemctl daemon-reload 25 26# 开机启动 27sudo systemctl enable supervisord.service

Systemd 管理服务:

1# 启用开机启动 2sudo systemctl enable supervisor 3# 关闭开机启动 4sudo systemctl disable supervisor 5 6# 启用服务 7systemctl start supervisor 8# 关闭服务 9systemctl stop supervisor

配置

查看配置

1$ cat /etc/supervisor/supervisord.conf 2; supervisor config file 3 4[unix_http_server] 5file=/var/run/supervisor.sock ; (the path to the socket file) 6chmod=0700 ; sockef file mode (default 0700) 7 8[supervisord] 9logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) 10pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) 11childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP) 12 13; the below section must remain in the config file for RPC 14; (supervisorctl/web interface) to work, additional interfaces may be 15; added by defining them in separate rpcinterface: sections 16[rpcinterface:supervisor] 17supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface 18 19[supervisorctl] 20serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket 21 22; The [include] section can just contain the "files" setting. This 23; setting can list multiple files (separated by whitespace or 24; newlines). It can also contain wildcards. The filenames are 25; interpreted as relative to this file. Included files *cannot* 26; include files themselves. 27 28[include] 29files = /etc/supervisor/conf.d/*.conf

配置说明

1$ echo_supervisord_conf 2; Sample supervisor config file. 3; 4; For more information on the config file, please see: 5; http://supervisord.org/configuration.html 6; 7; Notes: 8; - Shell expansion ("~" or "$HOME") is not supported. Environment 9; variables can be expanded using this syntax: "%(ENV_HOME)s". 10; - Comments must have a leading space: "a=b ;comment" not "a=b;comment". 11 12[unix_http_server] 13file=/tmp/supervisor.sock ; (the path to the socket file) 14;chmod=0700 ; socket file mode (default 0700) 15;chown=nobody:nogroup ; socket file uid:gid owner 16;username=user ; (default is no username (open server)) 17;password=123 ; (default is no password (open server)) 18 19;[inet_http_server] ; inet (TCP) server disabled by default 20;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface) 21;username=user ; (default is no username (open server)) 22;password=123 ; (default is no password (open server)) 23 24[supervisord] 25logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log) 26logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) 27logfile_backups=10 ; (num of main logfile rotation backups;default 10) 28loglevel=info ; (log level;default info; others: debug,warn,trace) 29pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid) 30nodaemon=false ; (start in foreground if true;default false) 31minfds=1024 ; (min. avail startup file descriptors;default 1024) 32minprocs=200 ; (min. avail process descriptors;default 200) 33;umask=022 ; (process file creation umask;default 022) 34;user=chrism ; (default is current user, required if root) 35;identifier=supervisor ; (supervisord identifier, default is 'supervisor') 36;directory=/tmp ; (default is not to cd during start) 37;nocleanup=true ; (don't clean up tempfiles at start;default false) 38;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP) 39;environment=KEY="value" ; (key value pairs to add to environment) 40;strip_ansi=false ; (strip ansi escape codes in logs; def. false) 41 42; the below section must remain in the config file for RPC 43; (supervisorctl/web interface) to work, additional interfaces may be 44; added by defining them in separate rpcinterface: sections 45[rpcinterface:supervisor] 46supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface 47 48[supervisorctl] 49serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket 50;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket 51;username=chris ; should be same as http_username if set 52;password=123 ; should be same as http_password if set 53;prompt=mysupervisor ; cmd line prompt (default "supervisor") 54;history_file=~/.sc_history ; use readline history if available 55 56; The below sample program section shows all possible program subsection values, 57; create one or more 'real' program: sections to be able to control them under 58; supervisor. 59 60;[program:theprogramname] 61;command=/bin/cat ; the program (relative uses PATH, can take args) 62;process_name=%(program_name)s ; process_name expr (default %(program_name)s) 63;numprocs=1 ; number of processes copies to start (def 1) 64;directory=/tmp ; directory to cwd to before exec (def no cwd) 65;umask=022 ; umask for process (default None) 66;priority=999 ; the relative start priority (default 999) 67;autostart=true ; start at supervisord start (default: true) 68;startsecs=1 ; # of secs prog must stay up to be running (def. 1) 69;startretries=3 ; max # of serial start failures when starting (default 3) 70;autorestart=unexpected ; when to restart if exited after running (def: unexpected) 71;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2) 72;stopsignal=QUIT ; signal used to kill process (default TERM) 73;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) 74;stopasgroup=false ; send stop signal to the UNIX process group (default false) 75;killasgroup=false ; SIGKILL the UNIX process group (def false) 76;user=chrism ; setuid to this UNIX account to run the program 77;redirect_stderr=true ; redirect proc stderr to stdout (default false) 78;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO 79;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 80;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10) 81;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) 82;stdout_events_enabled=false ; emit events on stdout writes (default false) 83;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO 84;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 85;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10) 86;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) 87;stderr_events_enabled=false ; emit events on stderr writes (default false) 88;environment=A="1",B="2" ; process environment additions (def no adds) 89;serverurl=AUTO ; override serverurl computation (childutils) 90 91; The below sample eventlistener section shows all possible 92; eventlistener subsection values, create one or more 'real' 93; eventlistener: sections to be able to handle event notifications 94; sent by supervisor. 95 96;[eventlistener:theeventlistenername] 97;command=/bin/eventlistener ; the program (relative uses PATH, can take args) 98;process_name=%(program_name)s ; process_name expr (default %(program_name)s) 99;numprocs=1 ; number of processes copies to start (def 1) 100;events=EVENT ; event notif. types to subscribe to (req'd) 101;buffer_size=10 ; event buffer queue size (default 10) 102;directory=/tmp ; directory to cwd to before exec (def no cwd) 103;umask=022 ; umask for process (default None) 104;priority=-1 ; the relative start priority (default -1) 105;autostart=true ; start at supervisord start (default: true) 106;startsecs=1 ; # of secs prog must stay up to be running (def. 1) 107;startretries=3 ; max # of serial start failures when starting (default 3) 108;autorestart=unexpected ; autorestart if exited after running (def: unexpected) 109;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2) 110;stopsignal=QUIT ; signal used to kill process (default TERM) 111;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) 112;stopasgroup=false ; send stop signal to the UNIX process group (default false) 113;killasgroup=false ; SIGKILL the UNIX process group (def false) 114;user=chrism ; setuid to this UNIX account to run the program 115;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners 116;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO 117;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 118;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10) 119;stdout_events_enabled=false ; emit events on stdout writes (default false) 120;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO 121;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 122;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10) 123;stderr_events_enabled=false ; emit events on stderr writes (default false) 124;environment=A="1",B="2" ; process environment additions 125;serverurl=AUTO ; override serverurl computation (childutils) 126 127; The below sample group section shows all possible group values, 128; create one or more 'real' group: sections to create "heterogeneous" 129; process groups. 130 131;[group:thegroupname] 132;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions 133;priority=999 ; the relative start priority (default 999) 134 135; The [include] section can just contain the "files" setting. This 136; setting can list multiple files (separated by whitespace or 137; newlines). It can also contain wildcards. The filenames are 138; interpreted as relative to this file. Included files *cannot* 139; include files themselves. 140 141;[include] 142;files = relative/directory/*.ini
  • inet_http_server: Web 管理页面

添加配置

1sudo -i 2cat <<-EOF > /etc/supervisor/conf.d/http_server.conf 3[program:HttpServer] 4directory=/home/john 5command=python3 -m http.server 9000 --bind 127.0.0.1 6priority=999 7autostart=true 8autorestart=true 9startsecs=10 10startretries=3 11stdout_logfile=/var/log/http_server_out.log 12stdout_logfile_maxbytes=1MB 13stdout_logfile_backups=10 14stdout_capture_maxbytes=1MB 15stderr_logfile=/var/log/http_server_err.log 16stderr_logfile_maxbytes=1MB 17stderr_logfile_backups=10 18stderr_capture_maxbytes=1MB 19environment= 20nocleanup=false 21EOF

更新配置

1supervisorctl update all

查看状态

1# supervisorctl status all 2HttpServer RUNNING pid 6826, uptime 0:00:15

访问 http://127.0.0.1:9000/ 可打开 HttpServer 页面。

参考

GoCoding 个人实践的经验分享,可关注公众号!

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

supervisor守护服务遇见的几个坑

近期在ubunt系列服务器上遇见了supervisor的几个坑,所以将服务守护都已经切换到systemd。坑一、资源限制配额不跟随limits.conf1.我们在用supervisor守护一个服务A的时候,发现由supervisor拉起的服务文件描述符未跟随系统limits设置。\program:servicea\username

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

CentOS 7安装Supervisor设置开机启动

SupervisorSupervistor是一个优秀的进程管理程序。它可以为系统UNIX服务器的运维人员,提供监控、操作服务进程的能力,可以作为长期运行的程序的坚实的守护进程。\Supvervisor本文为CentOS7系统的安装指南,成文于2019年02月16日,主要提供以下两个安装操作过程:

Linux进程管理工具 Supervisor详解

Supervisor安装与配置(linux/unix进程管理工具)Supervisor(http://supervisord.org(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fsupervisord.org))是用Python开发的一个client/server服务,是Li