supervisor使用总结

时间:2023-03-08 15:44:41

简介:

Supervisor是一个进程控制系统。 它是一个C/S系统(注意: 其提供WEB接口给用户查询和控制)。 它允许用户去监控和控制在类UNIX系统的进程。 它的目标与launchd、daemontools和runit有些相似。 但是与它们不一样的是、它不是作为init(进程号pid是1)运行。 它是被用来控制进程、并且它在启动的时候和一般程序并无二致。 那么通俗点,它的作用是什么? 你的Nginx,Tomcat,memcache,Redis...会崩么? 那你自己写的服务器监测脚本呢? 好吧、不要再纠结了、交给Supervisor吧! 它会帮你维护这些、即使它们不小心崩了、Supervisor会帮你看住它们、维护它们。

官网http://supervisord.org

 

一.安装

我的是CentOS Linux release 7.2.1511 所以就直接用yum install -y supervisor的方式来安装了

 

二.修改配置/etc/supervisor.conf

[unix_http_server]

file=/tmp/supervisor.sock   ; (the path to the socket file)

chmod=0777                 ; sockef file mode (default 0700)

[inet_http_server]         ; inet (TCP) server disabled by default (注意[inet_http_server]  前面的分号要去掉)

port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)

[supervisorctl]

serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket (注意前面是unix://)

serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket

 

三.启动过程中碰到的问题

启动

supervisord -c /etc/supervisord.conf

supervisorctl -c /etc/supervisord.conf

(

通过配置文件来启动supervisor,然后再使用supervisorctl, supervisord &supervisorctl是supervisor的两个可执行程序,其关系相当于Apache的httpd和apachectl;

supervisord是后台管理服务器,用来依据配置文件的的策略管理后台守护进程,会随系统自动启动。

supervisorctl用于管理员向后台管理员发送启动/重启/停止 等指令

)

问题1:

[root@localhost sunallies-pvm-center-refactor]# supervisorctl reload

error: <class 'socket.error'>, [Errno 111] Connection refused: file: /usr/lib64/python2.7/socket.py line: 571

没有启动supervisor的后台管理程序

supervisord -c /etc/supervisord.conf

supervisorctl -c /etc/supervisord.conf

 

问题2:

[root@localhost sunallies-pvm-center-refactor]# supervisorctl

http://127.0.0.1:9001 refused connection

如果已经开启supervisor(没启动也会有这种提示)且启用了127.0.0.1:9001端口则确认是否已经将[inet_http_server]前面的分号去掉

 

四.使用

1在[include]下添加定时任务的配置管理文件

[include]

files = supervisord.d/*.conf

例如我的一个laravel队列监控任务的配置如下:

/etc/supervisor/supervisord.d/pvm-api-queue.conf

[program:pvm-api-redis-queue-listener]    //程序名称,加粗部分可随便定义

process_name=%(program_name)s_%(process_num)02d

command=php /home/www/sunallies-pvm-api/artisan queue:work --queue pvm.api.profits  --sleep=1 --tries=3 --daemon     //任务的执行路径

autostart=true

autorestart=true

user=root

numprocs=2 //处理程序所使用的cpu进程数量

redirect_stderr=true

stdout_logfile=/home/www/sunallies-pvm-api/storage/logs/worker.log

[program:pvm-api-redis-warning-queue-listener]

process_name=%(program_name)s_%(process_num)02d

command=php /home/www/sunallies-pvm-api/artisan queue:work --queue pvm.warning  --sleep=1 --tries=3 --daemon

autostart=true

autorestart=true

user=root

numprocs=1

redirect_stderr=true

stdout_logfile=/home/www/sunallies-pvm-api/storage/logs/worker.log

[program:pvm-api-redis-makePlantEnergy-queue-listener]

process_name=%(program_name)s_%(process_num)02d

command=php /home/www/sunallies-pvm-api/artisan queue:work --queue pvm.api.makePlantEnergy --sleep=1 --tries=3 --daemon

autostart=true

autorestart=true

user=root

numprocs=3

redirect_stderr=true

stdout_logfile=/home/www/sunallies-pvm-api/storage/logs/worker.log

 

修改或者添加后台任务程序或者supervisor配置之后需要重新加载(reload)【最好重新启动(restart)】supervisor

 

supervisor使用总结

laravel queue详细说明https://www.lijinma.com/blog/2017/01/31/laravel-queue/

参考文档https://laravel-china.org/topics/2126/supervisor-installation-configuration-use