supervisor 安装配置详解

一、安装

  • 源码安装

先下载最新的supervisor安装包:https://pypi.python.org/pypi/supervisor , 如:

(python3命令为 pip install git+https://github.com/Supervisor/supervisor@master)

  1.  
    cd /usr/local/src
  2.  
    wget https://pypi.python.org/packages/7b/17/88adf8cb25f80e2bc0d18e094fcd7ab300632ea00b601cbbbb84c2419eae/supervisor-3.3.2.tar.gz
  3.  
    tar -zxvf supervisor-3.3.2.tar.gz
  4.  
    cd supervisor-3.3.2
  5.  
    python setup.py install #本地python版本为python2.7
  6.  
    # python2.7 setup.py install #本地python版本为python3以上

二、配置

1.生成配置文件

  1.  
    echo_supervisord_conf > /etc/supervisord.conf
  2.  
     

2.启动

supervisord -c /etc/supervisord.conf
 

查看 supervisord 是否在运行:

ps aux | grep supervisord
 

3.配置

打开配置文件

vim /etc/supervisord.conf
 

在最下边加入

[program:sougou]   #souogu 为程序的名称
command=scrapy crawl sougou #需要执行的命令
directory=/home/mzj/桌面/sougou/sougou/wechat_name/wechat_name/spiders  #命令执行的目录
environment=ASPNETCORE__ENVIRONMENT=Production #环境变量
user=root #用户
stopsignal=INT
autostart=true #是否自启动
autorestart=true #是否自动重启
startsecs=3 #自动重启时间间隔(s)
stderr_logfile=/home/mzj/桌面/sougou/sougou/wechat_name/wechat_name/spiders/ossoffical.err.log #错误日志文件
stdout_logfile=/home/mzj/桌面/sougou/sougou/wechat_name/wechat_name/spiders/ossoffical.out.log #输出日志文件

如报错:

Unlinking stale socket /tmp/supervisor.sock

 

unlink /tmp/supervisor.sock

在配置文件底部,配置include

  1.  
    [include]
  2.  
    files=/etc/supervisor/*.conf #若你本地无/etc/supervisor目录,请自建

用supervisor管理进程,配置如下:

  1.  
    cd /etc/supervisor
  2.  
    vim ossfs.conf # 这里的文件名称自定义

加入以下内容:

  1.  
    ; 设置进程的名称,使用 supervisorctl 来管理进程时需要使用该进程名
  2.  
    [program:your_program_name]
  3.  
    command=python server.py --port=9000
  4.  
    ;numprocs=1 ; 默认为1
  5.  
    ;process_name=%(program_name)s ; 默认为 %(program_name)s,即 [program:x] 中的 x
  6.  
    directory=/home/python/tornado_server ; 执行 command 之前,先切换到工作目录
  7.  
    user=oxygen ; 使用 oxygen 用户来启动该进程
  8.  
    ; 程序崩溃时自动重启,重启次数是有限制的,默认为3次
  9.  
    autorestart=true
  10.  
    redirect_stderr=true ; 重定向输出的日志
  11.  
    stdout_logfile = /var/log/supervisord/tornado_server.log
  12.  
    loglevel=info

这里是启动要配置的参数,请根据自己的项目自定义添加

更改了supervisor配置文件,需要重启,运行以下指令:

supervisorctl reload
 

4.supervisorctl的用法

  1.  
    supervisord : 启动supervisor
  2.  
    supervisorctl reload :修改完配置文件后重新启动supervisor
  3.  
    supervisorctl status :查看supervisor监管的进程状态
  4.  
    supervisorctl start 进程名 :启动XXX进程
  5.  
    supervisorctl stop 进程名 :停止XXX进程
  6.  
    supervisorctl stop all:停止全部进程,注:start、restart、stop都不会载入最新的配置文件。
  7.  
    supervisorctl update:根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启

5.若不使用控制台来管理进程,用浏览器来管理,该如何配置?

打开配置文件

vim /etc/supervisord.conf
 

配置 inet_http_server

  1.  
    [inet_http_server]
  2.  
    port=127.0.0.1:9001 ; 服务器ip
  3.  
    username=xxx ;自定义
  4.  
    password=xxx ;自定义

三、设置开机启动

vim /etc/init.d/supervisord
 

添加以下脚本

  1.  
    #! /bin/sh
  2.  
    ### BEGIN INIT INFO
  3.  
    # Provides: supervisord
  4.  
    # Required-Start: $remote_fs
  5.  
    # Required-Stop: $remote_fs
  6.  
    # Default-Start: 2 3 4 5
  7.  
    # Default-Stop: 0 1 6
  8.  
    # Short-Description: Example initscript
  9.  
    # Description: This file should be used to construct scripts to be
  10.  
    # placed in /etc/init.d.
  11.  
    ### END INIT INFO
  12.  
     
  13.  
    # Author: Dan MacKinlay <danielm@phm.gov.au>
  14.  
    # Based on instructions by Bertrand Mathieu
  15.  
    # http://zebert.blogspot.com/2009/05/installing-django-solr-varnish-and.html
  16.  
     
  17.  
    # Do NOT "set -e"
  18.  
     
  19.  
    # PATH should only include /usr/* if it runs after the mountnfs.sh script
  20.  
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
  21.  
    DESC="Description of the service"
  22.  
    NAME=supervisord
  23.  
    DAEMON=/usr/local/bin/supervisord
  24.  
    DAEMON_ARGS=" -c /etc/supervisord.conf"
  25.  
    #PIDFILE=/var/run/$NAME.pid
  26.  
    PIDFILE=/tmp/$NAME.pid
  27.  
    SCRIPTNAME=/etc/init.d/$NAME
  28.  
     
  29.  
    # Exit if the package is not installed
  30.  
    [ -x "$DAEMON" ] || exit 0
  31.  
     
  32.  
    # Read configuration variable file if it is present
  33.  
    [ -r /etc/default/$NAME ] && . /etc/default/$NAME
  34.  
     
  35.  
    # Load the VERBOSE setting and other rcS variables
  36.  
    . /lib/init/vars.sh
  37.  
     
  38.  
    # Define LSB log_* functions.
  39.  
    # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  40.  
    . /lib/lsb/init-functions
  41.  
     
  42.  
    #
  43.  
    # Function that starts the daemon/service
  44.  
    #
  45.  
    do_start()
  46.  
    {
  47.  
    # Return
  48.  
    # 0 if daemon has been started
  49.  
    # 1 if daemon was already running
  50.  
    # 2 if daemon could not be started
  51.  
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
  52.  
    || return 1
  53.  
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
  54.  
    $DAEMON_ARGS \
  55.  
    || return 2
  56.  
    # Add code here, if necessary, that waits for the process to be ready
  57.  
    # to handle requests from services started subsequently which depend
  58.  
    # on this one. As a last resort, sleep for some time.
  59.  
    }
  60.  
     
  61.  
    #
  62.  
    # Function that stops the daemon/service
  63.  
    #
  64.  
    do_stop()
  65.  
    {
  66.  
    # Return
  67.  
    # 0 if daemon has been stopped
  68.  
    # 1 if daemon was already stopped
  69.  
    # 2 if daemon could not be stopped
  70.  
    # other if a failure occurred
  71.  
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
  72.  
    RETVAL="$?"
  73.  
    [ "$RETVAL" = 2 ] && return 2
  74.  
    # Wait for children to finish too if this is a daemon that forks
  75.  
    # and if the daemon is only ever run from this initscript.
  76.  
    # If the above conditions are not satisfied then add some other code
  77.  
    # that waits for the process to drop all resources that could be
  78.  
    # needed by services started subsequently. A last resort is to
  79.  
    # sleep for some time.
  80.  
    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
  81.  
    [ "$?" = 2 ] && return 2
  82.  
    # Many daemons don't delete their pidfiles when they exit.
  83.  
    rm -f $PIDFILE
  84.  
    return "$RETVAL"
  85.  
    }
  86.  
     
  87.  
    #
  88.  
    # Function that sends a SIGHUP to the daemon/service
  89.  
    #
  90.  
    do_reload() {
  91.  
    #
  92.  
    # If the daemon can reload its configuration without
  93.  
    # restarting (for example, when it is sent a SIGHUP),
  94.  
    # then implement that here.
  95.  
    #
  96.  
    start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
  97.  
    return 0
  98.  
    }
  99.  
     
  100.  
    case "$1" in
  101.  
    start)
  102.  
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
  103.  
    do_start
  104.  
    case "$?" in
  105.  
    0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  106.  
    2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  107.  
    esac
  108.  
    ;;
  109.  
    stop)
  110.  
    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  111.  
    do_stop
  112.  
    case "$?" in
  113.  
    0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  114.  
    2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  115.  
    esac
  116.  
    ;;
  117.  
    #reload|force-reload)
  118.  
    #
  119.  
    # If do_reload() is not implemented then leave this commented out
  120.  
    # and leave 'force-reload' as an alias for 'restart'.
  121.  
    #
  122.  
    #log_daemon_msg "Reloading $DESC" "$NAME"
  123.  
    #do_reload
  124.  
    #log_end_msg $?
  125.  
    #;;
  126.  
    restart|force-reload)
  127.  
    #
  128.  
    # If the "reload" option is implemented then remove the
  129.  
    # 'force-reload' alias
  130.  
    #
  131.  
    log_daemon_msg "Restarting $DESC" "$NAME"
  132.  
    do_stop
  133.  
    case "$?" in
  134.  
    0|1)
  135.  
    do_start
  136.  
    case "$?" in
  137.  
    0) log_end_msg 0 ;;
  138.  
    1) log_end_msg 1 ;; # Old process is still running
  139.  
    *) log_end_msg 1 ;; # Failed to start
  140.  
    esac
  141.  
    ;;
  142.  
    *)
  143.  
    # Failed to stop
  144.  
    log_end_msg 1
  145.  
    ;;
  146.  
    esac
  147.  
    ;;
  148.  
    *)
  149.  
    #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
  150.  
    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
  151.  
    exit 3
  152.  
    ;;
  153.  
    esac
  154.  
     
  155.  
    :
  1.  
    # 设置该脚本为可以执行
  2.  
    sudo chmod +x /etc/init.d/supervisord
  3.  
    # 设置为开机自动运行
  4.  
    sudo update-rc.d supervisord defaults
  5.  
    # 试一下,是否工作正常
  6.  
    service supervisord stop
  7.  
    service supervisord start

若报错:insserv: warning: script 'service' missing LSB tags and overrides,请执行:

sudo apt-get remove insserv
 


--------------------- 本文来自 唐僧不爱八戒 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/python36/article/details/80571888?utm_source=copy 

posted @ 2018-10-08 15:02  vekair  阅读(4796)  评论(0编辑  收藏  举报