supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到supervisor的配置文件中就好了。此时被管理进程被视为supervisor的子进程,若该子进程异常中断,则父进程可以准确的获取子进程异常中断的信息,通过在配置文件中设置autostart=ture,可以实现对异常中断的子进程的自动重启。

安装supervisor

$ sudo apt-get install supervisor

配置文件

安装完supervisor后,输入以下命令可得到配置文件:

$ echo_supervisord_conf

或者:

$ cat /etc/supervisord/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

以上配置文件用到几个部分:

  • [unix_http_server]:这部分设置HTTP服务器监听的UNIX domain socket

    • file: 指向UNIX domain socket,即file=/var/run/supervisor.sock
    • chmod:启动时改变supervisor.sock的权限
  • [supervisord]:与supervisord有关的全局配置需要在这部分设置
    • logfile: 指向记录supervisord进程的log文件
    • pidfile:pidfile保存子进程的路径
    • childlogdir:子进程log目录设为AUTO的log目录
  • [supervisorctl]:
    • serverurl:进入supervisord的URL, 对于UNIX domain sockets, 应设为 unix:///absolute/path/to/file.sock
  • [include]:如果配置文件包含该部分,则该部分必须包含一个files键:
    • files:包含一个或多个文件,这里包含了/etc/supervisor/conf.d/目录下所有的.conf文件,可以在该目录下增加我们自己的配置文件,在该配置文件中增加[program:x]部分,用来运行我们自己的程序,如下:
  • [program:x]:配置文件必须包括至少一个program,x是program名称,必须写上,不能为空
    • command:包含一个命令,当这个program启动时执行
    • directory:执行子进程时supervisord暂时切换到该目录
    • user:账户名
    • startsecs:进程从STARING状态转换到RUNNING状态program所需要保持运行的时间(单位:秒)
    • redirect_stderr:如果是true,则进程的stderr输出被发送回其stdout文件描述符上的supervisord
    • stdout_logfile:将进程stdout输出到指定文件
    • stdout_logfile_maxbytes:stdout_logfile指定日志文件最大字节数,默认为50MB,可以加KB、MB或GB等单位
    • stdout_logfile_backups:要保存的stdout_logfile备份的数量

示例如下,在目录/etc/supervisor/conf.d/下创建awesome.conf,并加入:

;/etc/supervisor/conf.d/awesome.conf

[program:awesome]

command     = /usr/bin/env python3 /srv/awesome/www/app.py
directory = /srv/awesome/www
user = www-data
startsecs = 3 redirect_stderr = true
stdout_logfile_maxbytes = 50MB
stdout_logfile_backups = 10
stdout_logfile = /srv/awesome/log/app.log

配置完后,先进入/srv/awesome/目录下创建log目录,之后启动supervisor:

$ sudo supervisord -c supervisor.conf

supervisor基本命令(后四个命令可以省略“-c supervisor.conf”):

supervisord -c supervisor.conf                       通过配置文件启动supervisor
supervisorctl -c supervisor.conf status 查看状态
supervisorctl -c supervisor.conf reload 重新载入配置文件
supervisorctl -c supervisor.conf start [all]|[x] 启动所有/指定的程序进程
supervisorctl -c supervisor.conf stop [all]|[x] 关闭所有/指定的程序进程

执行服务(运行app.py):

$ sudo supervisorctl start awesome

如果supervisor遇到错误,可以在/var/log/supervisor/supervisord.log中查看日志;
如果app运行出现问题,可以在/srv/awesome/log/app.log中查看日志。

转: Linux --- Supervisor的作用与配置的更多相关文章

  1. Linux Supervisor 守护进程基本配置

    supervisor:C/S架构的进程控制系统,可使用户在类UNIX系统中监控.管理进程.常用于管理与某个用户或项目相关的进程. 组成部分supervisord:服务守护进程supervisorctl ...

  2. Supervisor的作用与配置

    supervisor supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到super ...

  3. 【收藏】Supervisor的作用与配置

    原文链接:https://www.jianshu.com/p/0226b7c59ae2 supervisor supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supe ...

  4. Linux版OpenVPN安装、配置教程(转)

    本文将以目前最新的openvpn-2.3.4.tar.gz(更新于2014-5-2,下载地址)为例来介绍如何在Linux系统中安装.配置及使用OpenVPN. 在这里,我们选用了一台预装CentOS ...

  5. linux下Java环境的配置

    linux下Java环境的配置 现在用linux的朋友越来越多了,前几天就有两个朋友问我linux下怎么配置java环境,我想还有很多朋友想了解学习这方面的东西,就写一个完全一点的linux java ...

  6. SUPERVISOR进程管理器配置指南

    SUPERVISOR进程管理器配置指南1. supervisor简介1.1. 官网http://supervisord.org/ 1.2. 介绍Supervisor是一个进程控制系统. 它是一个C/S ...

  7. Linux 操作系统下为网卡配置ip

    Linux操作系统下为网卡配置ip by:授客 QQ:1033553122 1.   Linux单一网卡设置多IP的配置方法 在Linux下网卡接口逻辑名被称为eth0,eth1,eth2,..... ...

  8. supervisor管理进程工具配置

    Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统 ...

  9. Linux Supervisor的安装与使用入门---SuSE

    Linux Supervisor的安装与使用入门 在linux或者unix操作系统中,守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事 ...

随机推荐

  1. 【linux】shell中命令替换$(cmd)和符号`cmd`

    来源:https://zhidao.baidu.com/question/485498670.html 作用: $(cmd)和`cmd`的作用是相同的,在执行一条命令时,会先将其中的 ``,或者是$( ...

  2. mongodb分页查询

    Limit与Skip方法 MongoDB Limit() 方法 如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指 ...

  3. CSS3 transition实现超酷图片墙动画效果

    一.前面的感慨以前也陆陆续续试过CSS3的一些特性,文字投影,多边框等.但都是试试而已,知道有这么回事.今天,见到了一个新玩意,transition,认认真真的试了一下,经过,我懵了,我呆了,我傻了, ...

  4. STM32 串口通信使用奇偶校验

    STM32串口通信如果使用奇偶校验,需要设置数据位长度为9bit USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USAR ...

  5. CSS------Filter属性的使用方法

    转载: http://www.w3cplus.com/css3/ten-effects-with-css3-filter

  6. MySQL连接表

    一:MySQL别名 1.介绍 使用MySQL别名来提高查询的可读性. MySQL支持两种别名,称为列别名和表别名. 有时,列的名称是一些表达式,使查询的输出很难理解.要给列一个描述性名称,可以使用列别 ...

  7. Python3安装turtle提示错误:Command "python setup.py egg_info" failed with error code 1

    Python3安装turtle提示错误:Command "python setup.py egg_info" failed with error code 1 Python3.5安 ...

  8. python django + js 使用ajax进行文件上传并获取上传进度案例

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 【HDU5909】Tree Cutting(FWT)

    [HDU5909]Tree Cutting(FWT) 题面 vjudge 题目大意: 给你一棵\(n\)个节点的树,每个节点都有一个小于\(m\)的权值 定义一棵子树的权值为所有节点的异或和,问权值为 ...

  10. Linux C语言编程学习笔记 (1)进程控制入门

    想进行Linux系统开发已经很久了,一直没有付诸实践.今日终于开始学习Linux下的C语言编程,研究一天,终于大概弄明白了Linux系统进程管理的一些基本概念和编程方法,总结下来以方便大家学习和自己实 ...