Linux supervisor 安装和使用入门

安装python环境

yum install python-setuptools

        

安装supervisor

easy_install supervisor

        

配置supervisor

mkdir -m 755 -p /etc/supervisor/

创建主配置文件

echo_supervisord_conf > /etc/supervisor/supervisord.conf

修改配置

/etc/supervisor/supervisord.conf

 

[unix_http_server]
file=/tmp/supervisor.sock;
#修改为:/etc/supervisor/supervisord.sock
[supervisord]
logfile=/tmp/supervisord.log;
#修改为:  /etc/supervisor/supervisord.log
pidfile=/tmp/supervisord.pid;
#pid文件 修改为:  /etc/supervisor/supervisord.pid
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock;
#修改为:  /etc/supervisor/supervisord.sock
#包含其它配置文件
[include]
files  relative/directory/*.ini;
#可以指定一个或多个以.ini结束的配置文件   修改为:/etc/supervisor/conf.d/*.ini

创建项目配置文件

cd /etc/supervisor/

mkdir -m 755 conf.d

cd conf.d

vim solve.ini  #项目名称命名

        

例子:

[program:solve]
process_name=%(program_name)s_%(process_num)02d
command=php /项目路径/artisan queue:work --timeout=60 --tries=3 --queue=QueueTest
autostart=true
autorestart=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/etc/supervisor/conf.d/worker.log #日志

开机自启supervisor服务

配置systemctl服务

进入/lib/systemd/system目录,并创建supervisord.service文件

cd /lib/systemd/system

vim /lib/systemd/system/supervisord.service

文件内容

[Unit]

Description=supervisord

After=network.target

[Service]

Type=forking

ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf

ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown

ExecReload=/usr/bin/supervisorctl $OPTIONS reload

KillMode=process

Restart=on-failure

RestartSec=42s

[Install]

WantedBy=multi-user.target

启动

systemctl enable supervisord.service
systemctl daemon-reload

查看是否启动

systemctl is-enabled supervisord

成功之后,就可以使用如下命令管理supervisor服务了

命令列表:

systemctl stop supervisord   #关闭
systemctl start supervisord  #启动
systemctl status supervisord  #查看状态
systemctl reload supervisord  #重新加载配置
systemctl restart supervisord   #重启

查看supervisor服务

ps -ef | grep supervisor

修改文件权限为766

chmod 766 /lib/systemd/system/supervisord.service

配置service类型服务

vim /etc/rc.d/init.d/supervisord
#!/bin/bash
#
# supervisord   This scripts turns supervisord on
#
# Author:       Mike McGrath <[email protected]> (based off yumupdatesd)
#
# chkconfig:    - 95 04
#
# description:  supervisor is a process control utility.  It has a web based
#               xmlrpc interface as well as a few other nifty features.
# processname:  supervisord
# config: /etc/supervisor/supervisord.conf
# pidfile: /var/run/supervisord.pid
#

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0

start() {
    echo -n $"Starting supervisord: "
    daemon "supervisord -c /etc/supervisor/supervisord.conf "
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/supervisord
}

stop() {
    echo -n $"Stopping supervisord: "
    killproc supervisord
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/supervisord
}

restart() {
    stop
    start
}

case "$1" in
  start)
    start
    ;;
  stop) 
    stop
    ;;
  restart|force-reload|reload)
    restart
    ;;
  condrestart)
    [ -f /var/lock/subsys/supervisord ] && restart
    ;;
  status)
    status supervisord
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
    exit 1
esac

exit $RETVAL

给脚本增加执行权限 设置开机自启

chmod 755 /etc/rc.d/init.d/supervisord

chkconfig supervisord on

/sbin/chkconfig on

问题:

因为supervisor使用的是root安装,所以,对于非root用户,如果在执行

supervisord -c /etc/supervisor/supervisord.conf

supervisorctl

命令时,会遇到访问被拒(Permission denied)的问题。

在命令最前面加上sudo即可

接下来开始开启项目的程序功能,也就是项目的队列监控,我是使用非root用户启动的

sudo supervisorctl reload #重新加载一下配置
sudo supervisorctl reread #读取
sudo supervisorctl update #更新
sudo supervisorctl start laravel-worker:* #开启

查看状态

     

正常进行中

调用一下,如果不知道怎么调用laravel的队列,请看我写的前两篇,这里不做介绍了。

调用后, redis中有新增Key

        

因为我是延迟2分钟的 所以等2分钟

通过linux的tail命令去看 刚才设置的日志文件

 

tail -f /etc/supervisor/conf.d/worker.log

        

发现执行后 数据库那一条数据 果然发生了改变

这样就实现了,守护进程去自动处理队列

献丑了,安装第二遍才成功

 

附赠 卸载命令

easy_install -m supervisor

 

借鉴文章:

https://blog.csdn.net/StriverFeng/article/details/80057304

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章