zoukankan      html  css  js  c++  java
  • 【已采纳】supervisor在服务器端(linux),如何一直运行你的python代码

    正式开始之前,说一下我的项目是放在虚拟环境里的,具体什么是虚拟环境,怎么创建,请自行百度噢!

     

    一、安装

    • 源码安装

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

       如:

      (python3命令为 pip install git+https://github.com/Supervisor/supervisor@master)或者pip install supervisor(pip2.7版本可用)

    1.  
      cd /usr/local/ENV
    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.生成配置文件

     echo_supervisord_conf > /etc/supervisord.conf

    2.启动

    supervisord -c /etc/supervisord.conf(这一步可以放在3.配置文件之后噢,可以先更改配置文件,再启动)
    
     

    查看 supervisord 是否在运行:

    ps aux | grep supervisord 如下图即可:

     

    3.配置

    打开配置文件

    vim /etc/supervisord.conf
     

    在最下边加入:举例子我的项目名称为:love.py 目录文件名为:lowerlove

    [program:lowerlove]   #lowerlove 为程序的名称
    command=python /usr/local/ENV/lowerlove/love.py  #需要执行的命令
    directory=/usr/local/ENV/lowerlove  #命令执行的目录
    environment=ASPNETCORE__ENVIRONMENT=Production #环境变量
    user=root   #用户
    stopsignal=10 #这个是当我们向子进程发送stopsignal信号后,到系统返回信息给supervisord,所等待的最大时间。 超过这个时间,supervisord会向该子进程发送一个强制kill的信号。根据自己项目性能情况实际修改

    autostart=true #是否自启动c  

    autorestart=true #是否自动重启

    startsecs=3 #自动重启时间间隔(s)
    stderr_logfile=/usr/local/ENV/lowerlove/love.err.log #错误日志文件
    stdout_logfile=/usr/local/ENV/lowerlove/love.out.log #输出日志文件

    如报错:

     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 "$1in
    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
     
     
    声明:本文为博主学习感悟总结,水平有限,如果不当,欢迎指正。如果您认为还不错,欢迎转载。转载与引用请注明作者及出处。
  • 相关阅读:
    Java之集合类应用总结
    Java的HashMap和HashTable
    Java安全通信:HTTPS与SSL
    字符串如何排序
    HashMap泛型编程
    Java之currenHashMap
    Android游戏开发:游戏框架的搭建(1)
    Java安全通信概述
    嵌入式linux学习步骤
    图片水平无缝滚动的代码
  • 原文地址:https://www.cnblogs.com/chaoyangya/p/11760212.html
Copyright © 2011-2022 走看看