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

 

一、安装

  • 源码安装

先下载最新的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 "$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
 
 
声明:本文为博主学习感悟总结,水平有限,如果不当,欢迎指正。如果您认为还不错,欢迎转载。转载与引用请注明作者及出处。

【已采纳】supervisor在服务器端(linux),如何一直运行你的python代码的更多相关文章

  1. linux进程后台运行的几种方法

    转载:http://hi.baidu.com/ntuxmzvdpzbnuxq/item/79131b93f606a348f0421562 我 们经常会碰到这样的问题,用 telnet/ssh 登录了远 ...

  2. linux下设置计划任务执行python脚本

    linux下设置计划任务执行python脚本 简介 crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自 ...

  3. linux 下 tomcat 运行报错 Broken pipe

    linux 下 tomcat 运行报错 Broken pipe 感谢:http://hi.baidu.com/liupenglover/blog/item/4048c23ff19f1cd67d1e71 ...

  4. 在Linux服务器上运行Jupyter notebook server教程

    在Linux服务器上运行Jupyter notebook server教程 很多deep learning教程都推荐在jupyter notebook运行python代码,方便及时交互.但只在本地运行 ...

  5. linux进程——后台运行的方法

    linux进程后台运行的几种方法: 我们经常会碰到这样的问题,用 telnet/ssh 登录了远程的 Linux 服务器,运行了一些耗时较长的任务, 结果却由于网络的不稳定导致任务中途失败. 如何让命 ...

  6. linux下编译运行TIGL Viewer步骤

    linux下编译运行TIGL Viewer步骤(仅为了正确编译安装的话直接跳到步骤3) 1. linux发行版选择:由于linux发行版众多,不同版本包含的库版本可能存在差别,因此需要选择正确的版本. ...

  7. linux中c语言和php语言通信代码UDP&TCP

    linux中c语言和php语言通信代码UDP&TCP http://blog.chinaunix.net/uid-24015214-id-2644174.html UDP方式通信   服务器端 ...

  8. 如何将.Net Core应用程序部署在Linux操作系统上运行

    .Net Core简介 跨平台: 可以在 Windows.macOS 和 Linux 操作系统上运行. 跨体系结构保持一致: 在多个体系结构(包括 x64.x86 和 ARM)上以相同的行为运行代码. ...

  9. 【转】Linux环境搭建FTP服务器与Python实现FTP客户端的交互介绍

    Linux环境搭建FTP服务器与Python实现FTP客户端的交互介绍 FTP 是File Transfer Protocol(文件传输协议)的英文简称,它基于传输层协议TCP建立,用于Interne ...

随机推荐

  1. PHP代码篇(二)-- array_column函数将二维数组格式化成固定格式的一维数组,及优化查询方法

    小白因为经常用到多表查询,比如获取一个会员领取的卡卷list,里面当然包含了1“会员优惠券记录表t_coupon_members”主表,然后2“门店优惠券表t_coupon”,和3“门店信息表t_sh ...

  2. Python—端口检测(socket)

    基于python检测端口是否在使用 原理:创建一个socket服务,连接对应的 ip:port ,如果能够连接,说明端口被占用:若端口可用,则不可连接. #!/usr/bin/evn python # ...

  3. nginx 配置实例-负载均衡

    1.实现效果 (1)浏览器地址栏输入地址 http://www.123.com/edu/a.html,负载均衡效果,平均 8080 和 8081 端口中 2.准备工作 (1)准备两台 tomcat 服 ...

  4. 201871010113-刘兴瑞《面向对象程序设计(java)》第六-七周学习总结

    项目 内容 这个作业属于哪个课程 <任课教师博客主页链接> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 <作业链接地址>htt ...

  5. SecureCRT自动断开连接的解决方法

    方法一: 在普通用户下,输入重启sshd服务的命令:service sshd restart 这时会提示:管理系统服务或单元需要身份验证.解决的方法:先要切换为root用户,接着重启sshd服务:se ...

  6. SpringMVC详细流程(一)

    Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模 ...

  7. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  8. CF1252J Tiling Terrace

    CF1252J Tiling Terrace 洛谷评测传送门 题目描述 Talia has just bought an abandoned house in the outskirt of Jaka ...

  9. Aug.2019_Memory

    转眼间这半个月的时光已经过去了,现在就像妈说的,生活又要回归正常了.尽管我一直不愿意去承认说前半个月的时光是一种不正常的生活,但有一点是我无法否认的,那就是那些人和那些事所带给我的…… 想想最初自己也 ...

  10. eclipse maven项目如何将所有的jar包复制到lib目录下?

      1.情景展示 我们知道,maven项目的jar包并不存在于项目当中,项目所需的jar包都保存在本地仓库中,如果本地仓库没有,会从配置的中央仓库下载,如果中央仓库也没有就会报错: 如上图所示,我想将 ...