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

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