• supervisor安装与基本使用


      supervisor简介

      一般的,我们部署一个项目,我们希望它能在挂了之后能自动重启,这时就要用守护进程了,而supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。

      运行 Supervisor 时会启动一个进程 supervisord,它负责启动所管理的进程,并将所管理的进程作为自己的子进程来启动,而且可以在所管理的进程出现崩溃时自动重启。

      supervisor安装

      我这里使用的是Ubuntu16.04,所以可以直接采用apt工具安装  

        sudo apt install supervisor

      安装完成后,在/etc目录下会有一个supervisor目录,里面有一个supervisord.conf文件和conf.d目录 ,supervisord.conf是supervisor的配置文件,conf.d是子进程的配置文件,打开supervisord.conf文件,内容如下:  

        ; supervisor config file
        
        [unix_http_server]
        file=/var/run/supervisor.sock   ; (the path to the socket file)
        chmod=0700                       ; sockef file mode (default 0700)
        
        [supervisord]
        logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
        pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
        childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)
        
        ; the below section must remain in the config file for RPC
        ; (supervisorctl/web interface) to work, additional interfaces may be
        ; added by defining them in separate rpcinterface: sections
        [rpcinterface:supervisor]
        supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
        
        [supervisorctl]
        serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket
        
        ; The [include] section can just contain the "files" setting.  This
        ; setting can list multiple files (separated by whitespace or
        ; newlines).  It can also contain wildcards.  The filenames are
        ; interpreted as relative to this file.  Included files *cannot*
        ; include files themselves.
        
        [include]
        files = /etc/supervisor/conf.d/*.conf

      后面files = /etc/supervisor/conf.d/*.conf即子进程配置文件应该放的目录,这里规定.conf结尾的文件将被当做配置文件,一般的,我们也是一个程序的配置放到一个文件中

      默认情况下supervisor是开机自动启动的,在/etc/systemd/system/multi-user.target.wants/supervisor.service(这个是一个软连接)中配置有supervisor启动程序  

        [Unit]
        Description=Supervisor process control system for UNIX
        Documentation=http://supervisord.org
        After=network.target
        
        [Service]
        ExecStart=/usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
        ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
        ExecReload=/usr/bin/supervisorctl -c /etc/supervisor/supervisord.conf $OPTIONS reload
        KillMode=process
        Restart=on-failure
        RestartSec=50s
        
        [Install]
        WantedBy=multi-user.target

      如果supervisor.service这个文件不存在,我们可以新建一个supervisor.service,并将上面的内容复制过去(注意路径),或者同样使用软连接。

        #先看supervisor.service是否已经加到开机启动
        sudo systemctl is-enabled supervisor.service
        #添加
        sudo systemctl enable supervisor.service
        #验证
        sudo systemctl is-enabled supervisor.service

      supervisor常用命令

      supervisor常用命令就是supervisord和supervisorctl。

      supervisord主要用来启动supervisor,查看supervisor的版本,启动用户等等,一般使用-c参数来指明启动使用的supervisor配置文件。

      supervisorctl主要用来管理子进程,因此这个命令要求supervisor是启动的,否则可能会抛出u【nix:///var/run/supervisor.sock no such file】的异常,这个时候只需要使用【sudo supervisord -c 配置文件路径】来启动supervisor,如果像上面设置了开机自启,可以使用【sudo systemctl start supervisor.service】来启动supervisor。  

        supervisorctl  #进入supervisor的交互界面
    
        help # 查看帮助
        status # 查看程序状态
        stop program_name # 关闭 指定的程序
        start program_name # 启动 指定的程序
        restart program_name # 重启 指定的程序
        tail -f program_name # 查看 该程序的日志
        reload # 重新启动配置中的程序(只是重启,不会加载修改过的配置)
        update # 重启配置文件修改过的程序(修改了配置,通过这个命令加载新的配置)
        
        #program_name为空或者为all,表示所有
        #也可以直接使用supervisorctl+命令的方式执行而不用进入交互界面,如:
       supervisorctl stop program_name # 关闭 指定的程序
       supervisorctlstart program_name # 启动 指定的程序

      supervisor子进程配置文件例子    

        #程序名称,终端控制时需要的标识
        [program:demo]
        # 运行程序的命令
        command=dotnet Demo.dll --server.urls=http://*:5000
        # 命令执行的目录
        directory=/home/feng/demo
        # 程序意外退出是否自动重启
        autorestart=true
        # 错误日志文件
        stderr_logfile=/var/log/Demo.err.log
        # 输出日志文件
        stdout_logfile=/var/log/Demo.out.log
        # 进程环境变量
        environment=
        # 进程执行的用户身份
        user=feng

      添加上面的子进程文件后,执行下面的命令加载    

        #加载配置,默认执行update后会自定启动
        sudo supervisorctl update
        # 查看程序状态
        sudo supervisorctl status
        #启动demo
        sudo supervisorctl start demo
  • 相关阅读:
    Java排序算法之归并排序
    Java多线程学习笔记(四)——Thread类中方法介绍
    大数据平台搭建:Hadoop
    np.arrange用法
    Pandas中DateFrame修改列名
    python .loc vs .iloc区别
    The categories of Reinforcement Learning 强化学习分类
    python3.6安装总结
    梯度下降法与牛顿下降法速度的比较
    My SQL 和SQL Server区别
  • 原文地址:https://www.cnblogs.com/shanfeng1000/p/12372061.html
Copyright © 2020-2023  润新知