supervisor是一个用 Python 写的进程管理工具,可以很方便的用来启动、重启、关闭进程(守护进程)。可以用他来管理自己的“服务程序”。
安装
首先安装Python,Mac系统好像自带。
执行 sudo pip install supervisor
安装
安装pip
下载get-pip.py,然后执行。具体请查看文档
$ wget https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py
...
$ sudo python get-pip.py
配置
使用默认配置项
$ sudo echo_supervisord_conf > /etc/supervisord.conf
$ sudo vim /etc/supervisord.conf
ps: 如果找不到echo_supervisord_conf
, 我去网上找了一份sample.
; Sample supervisor config file.
[unix_http_server]
file=/var/run/supervisor/supervisor.sock ; (the path to the socket file)
;chmod=0700 ; sockef 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.0:9001 ; (ip_address:port specifier, *:port for all iface)
;username=hello ; (default is no username (open server))
;password=world ; (default is no password (open server))
[supervisord]
logfile=/var/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]
serverurl=unix:///var/run/supervisor/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 http_username if set
;password=123 ; 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:theprogramname]
;command=/bin/cat ; 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=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=999 ; the relative start priority (default 999)
;autostart=true ; start at supervisord start (default: true)
;autorestart=true ; retstart at unexpected quit (default: true)
;startsecs=10 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; 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=/a/path ; 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.
;[eventlistener:theeventlistenername]
;command=/bin/eventlistener ; 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)
;events=EVENT ; event notif. types to subscribe to (req'd)
;buffer_size=10 ; event buffer queue size (default 10)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=-1 ; the relative start priority (default -1)
;autostart=true ; start at supervisord start (default: true)
;autorestart=unexpected ; restart at unexpected quit (default: unexpected)
;startsecs=10 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; 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_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A=1,B=2 ; process environment additions
;serverurl=AUTO ; override serverurl computation (childutils)
; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.
;[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 = supervisord.d/*.ini
其他可以暂时忽略,修改最底下一行
; 包含其他的配置文件
[include]
files = /etc/supervisor/*.conf ; 可以随意指定,目录不存在请先建立。配置文件可以是 *.conf 或 *.ini
测试一下
supervisord -c /etc/supervisord.conf
ps aux | grep supervisord
出现报错:
Error: Cannot open an HTTP server: socket.error reported errno.ENOENT (2)
这是因为配置文件中的socks路径不存在
file=/var/supervisor/supervisor.sock ; (the path to the socket file)
新建或设置其他位置
配置“服务”
这里我用“IDEA License Server”做示例
sudo vim /etc/supervisor/idea.conf
文件内容
[program:idea] ; 是应用程序的唯一标识,不能重复
directory = /data/jidea-server ; 程序的启动目录
command = /data/jidea-server/IntelliJIDEALicenseServer_darwin_amd64 ; 启动命令
autostart = true ; 在 supervisord 启动的时候也自动启动
startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true ; 程序异常退出后自动重启
startretries = 3 ; 启动失败自动重试次数,默认是 3
redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 20
stdout_logfile = /var/log/supervusor/jidea-server.log ; stdout 日志文件,注意:要确保目录已经建立并且可以访问(写权限)
使用命令supervisorctl -c /etc/supervisord.conf
加装并启动。如果一切正常可以使用命令supervisorctl status
查看状态。例如:
$ supervisorctl status
idea RUNNING pid 1177, uptime 0:32:00
$
以上输出表示一起正常,如果有错误,请“具体情况具体分析”本文档暂不做相关讨论。
使用 launchctl 来启动 supervisor 自身
launchctl
是Mac自带的工具,具体使用方法请看官方文档或者问度娘。
这里我在 /Library/LaunchAgents
目录下, 创建一个 supervisord.plist
文件, 命令:sudo vim /Library/LaunchDaemons/supervisord.plist
,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>Label</key>
<string>supervisord</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/supervisord</string>
<string>-n</string>
<string>-c</string>
<string>/etc/supervisord.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
注意:检查文件的权限
$ ls -lsa /Library/LaunchDaemons
total 24
0 drwxr-xr-x 5 root wheel 170 11 22 09:44 .
0 drwxr-xr-x+ 67 root wheel 2278 11 16 14:41 ..
8 -rw-r--r-- 1 root wheel 590 11 16 17:52 supervisord.plist
supervisord.plist
必须是属于root
用户的。不是的话修改:sudo chown root:wheel /Library/LaunchDaemons/supervisord.plist
。最后启动他
sudo launchctl load /Library/LaunchDaemons/supervisord.plist
注意,在启动前先检查一下supervisord
时不时已经在运行了,如果已经运行请先kill
掉。
注意: 必须在这个目录(/Library/LaunchDaemons
)下才会使用root
启动。
$ ps aux | grep supervisord
user 1167 0.0 0.2 4304600 15744 ?? Ss 9:52上午 0:00.51 /usr/bin/python /usr/local/bin/supervisord -n -c /etc/supervisord.conf
$ kill -4 1167