supervisor简单配置

本节内容:

  1. supervisor简单介绍
  2. 配置内容
  3. 启动过程
  4. 错误处理
  5. 我的配置文件
  6. 参考文章

一.supervisor简单介绍

  折腾了好几天,supervisor终于能行了,折腾死了,所以写这篇博客,把遇到的坑记下来,省的下次再弄不上

  • 环境:Ubuntu 16.04 LTS server python 2.7 
  • apt-get install supervisor就能安装上了
  • 以命令行的方式创建默认配置文件  echo_supervisord_conf > /etc/supervisor/supervisord.conf   ,注意这里是保存配置的位置

二.配置内容

  打开配置文件  

[unix_http_server]                                 #supervisord的unix_socket服务配置
file=/var/run/supervisor.sock                      #指定socket文件的保存目录,这里要修改成当前的目录,如果是缺省配置容易被linux清掉。
;chmod=0700                                        #socket的文件权限   
;chown=nobody:nogroup                              #socket的拥有者和组名
;username=user                                     #默认不需要登录用户
;password=123                                      #默认不需要登录密码

;[inet_http_server]                                #supervisord的tcp服务设置
;port=127.0.0.1:9001                               #tcp端口
;username=user                                     #tcp登录用户
;password=123                                      #tcp登录密码

  supervisor是C/S模式运行的,客服端负责向服务器端发送命令,服务器端负责根据客服端的命令和配置文件对进程进程调度。

  而服务端和客户端有两种通信模式,一种是通过unix_http_server配置的tcp套接字,一种是通过inet_http_server配置的端口通信模式。

  如果supervisor要正常工作,那unix_http_server和inet_http_server两个只要有一个配置正确就可以了,然后客户端supervisorctl配置同样的通信模式就可以了。

  

  注意:配置文件中/tmp开头的都要改掉,因为tmp是linux存放临时文件的地方随时会清掉   

  接下来是[supervisorctl],默认的配置是:

  

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock  #使用socket方式 ;serverurl=http://127.0.0.1:9001    #使用指定tcp方式 ;usernme=chris      #如果设置应该和http_username相同 ;password=123       #如果设置应该和http_password相同 ;prompt=mysupervisor              #命令行提示符,默认是"supervisor" ;history_file=~/.sc_history #命令行历史记录

  这里客户端的配置就是为了和服务器端对接,所以你可以看到serveurl有两种形式,一种是tcp,一种是端口,对应我们上面讲到

  的服务器端的两种方式,所以这里我们使用tcp方式的时候,把第一条去掉注释就可以了。

  同时我们这里username和password要配置成和上面unit_http_server一样的,不然账号密码不一样supervisorctl就无法和服务器通信

  最后要修改[include]

[include]
files = /etc/supervisor/conf.d/*.conf

  在这个路径下,可以配置我们的各个进程的配置

  新建一个example.conf

  配置如下:

[program:example]
directory=/root/ #选择在哪个目录执行,还挺重要的,防止代码定位位置时出错
command
=/usr/bin/python3 example.py     #命令执行 autostart=true                  #自动启动,随着supervisor启动而启动 autorestart=true                 #假如挂掉自动重启 user=root                     #以哪个用户执行
stdout_logfile=/var/log/supervisor.log #设置代码打印结果输出文件
stderr_logfile=/var.log/supervisor_error.log #设置代码报错结果的输出文件,排错好助手

三.启动过程

  首先启动服务端

    supervisord -c /etc/supervisor/supervisord.conf

    systemctl start supervisord.service

  客户端使用的两种方式:

    1.直接在终端内使用

获得所有程序状态 supervisorctl status
关闭目标程序   supervisorctl stop spider
启动目标程序   supervisorctl start spider
关闭所有程序   supervisorctl shutdown

    2.进入supervisorctl客户端交互模式

$ supervisorctl

supervisor> help

default commands (type help <topic>):
=====================================
add    exit      open  reload  restart   start   tail
avail  fg        pid   remove  shutdown  status  update
clear  maintail  quit  reread  signal    stop    version

supervisor> start example

  设置为开机启动,在/lib/systemd/system/中添加supervisord.service文件

[Unit]
Description=Supervisord Service

[Service]
Restart=on-failure
RestartSec=42s
User=root
ExecStart=/usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf

[Install]

  执行命令

sudo systemctl daemon-reload

sudo systemctl enable supervisord.service

sudo systemctl start supervisord.service

四.错误处理

  1.unix:///tmp/supervisor.sock no such file,基本上必会出现这个错误

    打开/etc/supervisor/supervisord.conf,这里把所有的/tmp路径改掉,/tmp/supervisor.sock 改成 /var/run/supervisor.sock,

    /tmp/supervisord.log改成 /var/log/supervisor.log,/tmp/supervisord.pid 改成 /var/run/supervisor.pid 要不容易被linux自动清掉,

    修改权限

    chmod 777 /var/run/

    chmod 777 /var/log

    创建supervisor.sock

    sudo touch /var/run/supervisor.sock

    sudo chmod 777 /var/run/supervisor.sock

    重启supervisord

    systemctl restart supervisord

  2.Unlinking stale socket /var/run/supervisor.sock   

    find / -name supervisor.sock

    unlink /***/supervisor.sock

五.我的配置文件

  我的配置文件,试验过没有问题的配置文件

六.参考文章

  supervisor安装概述    

  supervisor-Python进程管理工具

  解决unix:///tmp/supervisor.sock no such file的问题

  "unix:///tmp/supervisor.sock no such file" 错误处理

  supervisorctl 3.3.1 http://localhost:9001 refused connection

原文地址:https://www.cnblogs.com/gaodp/p/9550167.html