supervisor:C/S架构的进程控制系统,可使用户在类UNIX系统中监控、管理进程。常用于管理与某个用户或项目相关的进程。
组成部分
supervisord:服务守护进程
supervisorctl:命令行客户端
Web Server:提供与supervisorctl功能相当的WEB操作界面
XML-RPC Interface:XML-RPC接口
安装
easy_install supervisor
创建默认的配置文件
echo_supervisord_conf >/etc/supervisord.conf vi /etc/supervisord.conf
修改配置文件如下,这个例子设置了一个Nginx和asp.net core的守护进程
1 ; Sample supervisor config file. 2 ; 3 ; For more information on the config file, please see: 4 ; http://supervisord.org/configuration.html 5 ; 6 ; Notes: 7 ; - Shell expansion ("~" or "$HOME") is not supported. Environment 8 ; variables can be expanded using this syntax: "%(ENV_HOME)s". 9 ; - Comments must have a leading space: "a=b ;comment" not "a=b;comment". 10 11 ;[unix_http_server] 12 ;file=/tmp/supervisor.sock ; (the path to the socket file) 13 ;chmod=0700 ; socket file mode (default 0700) 14 ;chown=nobody:nogroup ; socket file uid:gid owner 15 ;username=user ; (default is no username (open server)) 16 ;password=123 ; (default is no password (open server)) 17 18 [inet_http_server] ; inet (TCP) server disabled by default 19 port=127.0.0.1:8888 ; (ip_address:port specifier, *:port for all iface) 20 username=user ; (default is no username (open server)) 21 password=123 ; (default is no password (open server)) 22 23 [supervisord] 24 logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log) 25 logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) 26 logfile_backups=10 ; (num of main logfile rotation backups;default 10) 27 loglevel=info ; (log level;default info; others: debug,warn,trace) 28 pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid) 29 nodaemon=false ; (start in foreground if true;default false) 30 minfds=1024 ; (min. avail startup file descriptors;default 1024) 31 minprocs=200 ; (min. avail process descriptors;default 200) 32 ;umask=022 ; (process file creation umask;default 022) 33 ;user=chrism ; (default is current user, required if root) 34 ;identifier=supervisor ; (supervisord identifier, default is 'supervisor') 35 ;directory=/tmp ; (default is not to cd during start) 36 ;nocleanup=true ; (don't clean up tempfiles at start;default false) 37 ;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP) 38 ;environment=KEY="value" ; (key value pairs to add to environment) 39 ;strip_ansi=false ; (strip ansi escape codes in logs; def. false) 40 41 ; the below section must remain in the config file for RPC 42 ; (supervisorctl/web interface) to work, additional interfaces may be 43 ; added by defining them in separate rpcinterface: sections 44 [rpcinterface:supervisor] 45 supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface 46 47 [supervisorctl] 48 serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket 49 ;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket 50 ;username=chris ; should be same as http_username if set 51 ;password=123 ; should be same as http_password if set 52 ;prompt=mysupervisor ; cmd line prompt (default "supervisor") 53 ;history_file=~/.sc_history ; use readline history if available 54 55 ; The below sample program section shows all possible program subsection values, 56 ; create one or more 'real' program: sections to be able to control them under 57 ; supervisor. 58 59 ;[program:theprogramname] 60 ;command=/bin/cat ; the program (relative uses PATH, can take args) 61 ;process_name=%(program_name)s ; process_name expr (default %(program_name)s) 62 ;numprocs=1 ; number of processes copies to start (def 1) 63 ;directory=/tmp ; directory to cwd to before exec (def no cwd) 64 ;umask=022 ; umask for process (default None) 65 ;priority=999 ; the relative start priority (default 999) 66 ;autostart=true ; start at supervisord start (default: true) 67 ;startsecs=1 ; # of secs prog must stay up to be running (def. 1) 68 ;startretries=3 ; max # of serial start failures when starting (default 3) 69 ;autorestart=unexpected ; when to restart if exited after running (def: unexpected) 70 ;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2) 71 ;stopsignal=QUIT ; signal used to kill process (default TERM) 72 ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) 73 ;stopasgroup=false ; send stop signal to the UNIX process group (default false) 74 ;killasgroup=false ; SIGKILL the UNIX process group (def false) 75 ;user=chrism ; setuid to this UNIX account to run the program 76 ;redirect_stderr=true ; redirect proc stderr to stdout (default false) 77 ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO 78 ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 79 ;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10) 80 ;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) 81 ;stdout_events_enabled=false ; emit events on stdout writes (default false) 82 ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO 83 ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 84 ;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10) 85 ;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) 86 ;stderr_events_enabled=false ; emit events on stderr writes (default false) 87 ;environment=A="1",B="2" ; process environment additions (def no adds) 88 ;serverurl=AUTO ; override serverurl computation (childutils) 89 90 ; The below sample eventlistener section shows all possible 91 ; eventlistener subsection values, create one or more 'real' 92 ; eventlistener: sections to be able to handle event notifications 93 ; sent by supervisor. 94 95 ;[eventlistener:theeventlistenername] 96 ;command=/bin/eventlistener ; the program (relative uses PATH, can take args) 97 ;process_name=%(program_name)s ; process_name expr (default %(program_name)s) 98 ;numprocs=1 ; number of processes copies to start (def 1) 99 ;events=EVENT ; event notif. types to subscribe to (req'd) 100 ;buffer_size=10 ; event buffer queue size (default 10) 101 ;directory=/tmp ; directory to cwd to before exec (def no cwd) 102 ;umask=022 ; umask for process (default None) 103 ;priority=-1 ; the relative start priority (default -1) 104 ;autostart=true ; start at supervisord start (default: true) 105 ;startsecs=1 ; # of secs prog must stay up to be running (def. 1) 106 ;startretries=3 ; max # of serial start failures when starting (default 3) 107 ;autorestart=unexpected ; autorestart if exited after running (def: unexpected) 108 ;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2) 109 ;stopsignal=QUIT ; signal used to kill process (default TERM) 110 ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) 111 ;stopasgroup=false ; send stop signal to the UNIX process group (default false) 112 ;killasgroup=false ; SIGKILL the UNIX process group (def false) 113 ;user=chrism ; setuid to this UNIX account to run the program 114 ;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners 115 ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO 116 ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 117 ;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10) 118 ;stdout_events_enabled=false ; emit events on stdout writes (default false) 119 ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO 120 ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) 121 ;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10) 122 ;stderr_events_enabled=false ; emit events on stderr writes (default false) 123 ;environment=A="1",B="2" ; process environment additions 124 ;serverurl=AUTO ; override serverurl computation (childutils) 125 126 ; The below sample group section shows all possible group values, 127 ; create one or more 'real' group: sections to create "heterogeneous" 128 ; process groups. 129 130 ;[group:thegroupname] 131 ;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions 132 ;priority=999 ; the relative start priority (default 999) 133 134 ; The [include] section can just contain the "files" setting. This 135 ; setting can list multiple files (separated by whitespace or 136 ; newlines). It can also contain wildcards. The filenames are 137 ; interpreted as relative to this file. Included files *cannot* 138 ; include files themselves. 139 140 ;[include] 141 ;files = relative/directory/*.ini 142 [program:WebApp1MVC] 143 command=dotnet /home/wilson/temp/PublishOutput/WebApp1.dll 144 directory=/home/wilson/temp/PublishOutput/ 145 autostart=true 146 autorestart=true 147 stderr_logfile=/var/log/hellomvc.err.log 148 stdout_logfile=/var/log/hellomvc.out.log 149 environment=ASPNETCORE_ENVIRONMENT=Production 150 user=wilson 151 stopsignal=INT 152 153 154 [program:Nginx] 155 command=/usr/local/nginx/sbin/nginx 156 directory=/usr/local/nginx/ 157 autostart=true 158 autorestart=true 159 stderr_logfile=/usr/local/nginx/logs/nginx.err.log 160 stdout_logfile=/usr/local/nginx/logs/nginx.out.log 161 user=wilson 162 stopsignal=INT
关键配置如下:
1 [program:WebApp1MVC] 2 command=/usr/bin/dotnet /home/wilson/temp/PublishOutput/WebApp1.dll 3 directory=/home/wilson/temp/PublishOutput/ 4 autostart=true 5 autorestart=true 6 stderr_logfile=/var/log/hellomvc.err.log 7 stdout_logfile=/var/log/hellomvc.out.log 8 environment=ASPNETCORE_ENVIRONMENT=Production 9 user=www-data 10 stopsignal=INT 11 12 13 14 15 [program:Nginx] 16 command=/usr/local/nginx/sbin/nginx 17 directory=/usr/local/nginx/ 18 autostart=true 19 autorestart=true 20 stderr_logfile=/usr/local/nginx/logs/nginx.err.log 21 stdout_logfile=/usr/local/nginx/logs/nginx.out.log 22 user=wilson 23 stopsignal=INT
启动Supervisor
supervisord -c /etc/supervisord.conf
浏览器中输入http://127.0.0.1:8888,输入用户名密码,就可以看到后台进程列表
参考:
https://docs.asp.net/en/latest/publishing/linuxproduction.html#
http://supervisord.org/installing.html#internet-installing-with-setuptools