安裝
可以用Python的pip工具進行安裝
sudo pip install supervisor
也可以使用Linux的包管理命令安裝
sudo apt-get install supervisor
配置
運行如下命令可以查看默認的配置
echo_supervisord_conf
如果需要修改默認的配置,可以運行下面命令生成一個默認的配置文件
echo_supervisord_conf > /etc/supervisord.conf
修改默認配置
默認配置文件中的
supervisord.sock
躯畴、supervisord.log
以及supervisord.pid
是放在/tmp目錄下影涉,這個目錄存放的是Linux中的臨時文件痪寻,一旦被系統(tǒng)刪除硝桩,就會提示unix:///tmp/supervisor.sock no such file
笙僚,所以我們要把這三個文件放到其他目錄中保存成福。
此處注意中文注釋的幾個需要修改的地方
[unix_http_server]
;此處修改為/var/run目錄碾局,避免被系統(tǒng)刪除
file=/var/run/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)
;[inet_http_server] ; inet (TCP) server disabled by default
;port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
[supervisord]
;此處修改為/var/log/supervisor目錄
logfile=/var/log/supervisor/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
;此處修改為/var/run目錄
pidfile=/var/run/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 rpcinterface:supervisor 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:x] sections.
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
; The supervisorctl section configures how supervisorctl will connect to
; supervisord. configure it match the settings in either the unix_http_server
; or inet_http_server section.
[supervisorctl]
;此處是關(guān)鍵,否則執(zhí)行supervisorctl status會爆出unix:// not found錯誤
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=chris ; should be same as in [*_http_server] if set
;password=123 ; should be same as in [*_http_server] if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The sample program section below 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)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; 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=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;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 (0 means none, 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 (0 means none, 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 sample eventlistener section below shows all possible eventlistener
; subsection values. Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.
;[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)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; autorestart 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=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners
;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 (0 means none, 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=10 ; # of stderr logfile backups (0 means none, 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 sample group section below 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净当,新增的配置將在/etc/supervisord.d/conf.d目錄下并以.conf結(jié)尾
[include]
files = /etc/supervisord.d/conf.d/*.conf
上面的一些目錄如果做了修改首先要創(chuàng)建這些目錄,并且賦予相應(yīng)的權(quán)限
sudo chmod 777 /var/run
sudo chmod 777 /var/log/supervisor
現(xiàn)在我們啟動服務(wù)
supervisord -c /etc/supervisord.conf
此處最好別用root權(quán)限啟動蕴潦,否則可能導(dǎo)致后面php artisan命令出錯
現(xiàn)在我們查看進程
pgrep supervisord | xargs ps -u --pid
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 13744 0.0 1.7 226320 17460 ? Ss 1月12 0:08 /bin/python /usr/bin/supervisord -c /etc/supervisord.conf
具體項目配置
Supervisorctl 是 Supervisord 的一個命令行客戶端工具像啼,啟動時需要指定與 Supervisord 使用同一份配置文件
我們在上面默認配置文件中加入了[include]
,所以我們在/etc/supervisord.d/conf.d
下新建一個配置文件larashop-worker.conf
[program:larashop-worker] ;項目名稱
process_name=%(program_name)s_%(process_num)02d
command=php (填入你的artisan路徑)/artisan queue:work redis --sleep=3 --tries=3 ;需要啟動的命令
autostart=true
autorestart=true
user=root ;此處填入你運行WEB應(yīng)用的用戶
numprocs=8 ;進程數(shù)
redirect_stderr=true ;把 stderr 重定向到 stdout品擎,默認 false
stdout_logfile=/var/log/supervisor/larashop-queue.log ;注意分配好日志文件夾權(quán)限
配置完成后我們執(zhí)行
supervisorctl reread # 讀取有更新(增加)的配置文件埋合,不會啟動新添加的程序
supervisorctl update # 重啟配置文件修改過的程序
supervisorctl start larashop-worker:* # 啟動 larashop-worker 程序
運行
supervisorctl status # 查看狀態(tài)
看到
larashop-worker:larashop-worker_00 RUNNING pid 13769, uptime 14:03:33
larashop-worker:larashop-worker_01 RUNNING pid 13770, uptime 14:03:33
larashop-worker:larashop-worker_02 RUNNING pid 13767, uptime 14:03:33
larashop-worker:larashop-worker_03 RUNNING pid 13768, uptime 14:03:33
larashop-worker:larashop-worker_04 RUNNING pid 13765, uptime 14:03:33
larashop-worker:larashop-worker_05 RUNNING pid 13766, uptime 14:03:33
larashop-worker:larashop-worker_06 RUNNING pid 13763, uptime 14:03:33
larashop-worker:larashop-worker_07 RUNNING pid 13764, uptime 14:03:33
說明Laravel隊列已經(jīng)開始正常運行了
如果在Laravel中修改了隊列代碼,需要重啟Supervisor才能生效
可能出現(xiàn)的問題
1. ERROR (spawn error)
此時如果提示
larashop-worker:larashop-worker_00: ERROR (spawn error)
...
先檢查上述的日志目錄有沒有權(quán)限萄传,如果權(quán)限正常甚颂,則進入上面設(shè)置的日志目錄查看日志文件
我這里進入var/log/supervisor
查看日志文件larashop-queue.conf
可以看到錯誤均為
PHP Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in xxx/artisan on line 33
然后我進入Laravel目錄,使用
sudo php artisan 你的命令
執(zhí)行artisan命令秀菱,同樣爆出這個錯誤振诬。所以原因在于啟動supervisord
和服務(wù)端的時候使用了root權(quán)限導(dǎo)致
先將原來的進程kill掉,使用非root權(quán)限重新執(zhí)行服務(wù)端和客戶端即可成功運行
2. unix:///tmp/supervisor.sock no such file
在服務(wù)端配置文件中將/tmp/supervisor.sock
修改為var/run/supervisor.sock
3. supervisorctl start錯誤
執(zhí)行supervisorctl start 你的項目名稱
衍菱,遲遲不出結(jié)果赶么,按ctrl+c
后爆出錯誤
Traceback (most recent call last):
File "/bin/supervisorctl", line 11, in <module>
...
建議kill掉supervisord
的進程,然后重新用pip安裝一次脊串,并重新配置一遍默認配置文件