Supervisor

一.介绍Supervisor

Supervisor在百度百科上给的定义是超级用户,监管员。Supervisor是一个进程管理工具,当进程中断的时候Supervisor能自动重新启动它。可以运行在各种类unix的机器上,supervisor就是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。
注意,是一个管理工具,并不是库或包。
Supervisor进程管理工具可以高效简单地对单个或者多个进程进行统一管理,如启动、重启、停止进程。更重要的作用是能在进程因为某种原因崩溃时,做到自动重启

1.后台(守护)、前台进程

​什么是守护进程?通俗点讲就是后台跑着的进程,不会因为你关了终端服务就会随之停止,直到你把计算机的电源关闭。当进程变为后台进程后,同一个终端就会释放了,你可以在其中干别的事情,而不会干扰到你跑的服务。后台进程一般来说不能捕捉输入,服务的输出依然可以选择在终端输出。
​  同样的道理,理解前台进程就简单多了,前台进程就是你的一个服务在跑,但是不能再在同一个终端干别的事情,必须把你现有的服务给停掉之后才可以继续干别的事,前台进程可以捕捉输入、输出。

2.Supervisor的安装

安装命令

sudo apt-get install supervisor

3.supervisor的配置文件

安装后,会发现在/etc/supervisor/路径下存在这样一个配置文件supervisor.conf
打开这个文件

gedit /etc/supervisor/supervisor.conf

发现supervisor.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
  • “;”分号后面的是注释
  • 其他的配置语句可以自行学习,这里关注最后一句话
[include]
files = /etc/supervisor/conf.d/*.conf

这句话的作用是:我们可以配置自己想要启动的进程

4.配置想要启动的进程

我们有两种方式来配置

  1. 写成单独的配置文件.conf
  2. 写在supervisor.conf里

4.1第一种:写成单独的配置文件.conf

需要supervisor.conf文件中包含这句话

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

比如我们有一个进程detect,它位于某个路径之下,你可以把下面这些话写在 /etc/supervisor/conf.d/路径下的detect.conf文件中,文件.conf前的名字可以自行设定

[program:detect]
command=/home/chen/detect/build/detect   ;进程的启动命令(detect是一个可执行进程)
process_name=%(program_name)s  ;进程名字(这是默认的写法)
numprocs=1 ;启动几个该进程
autostart=true ;在supervisord启动时也自动启动
autorestart=true ;程序异常退出后自动重启
directory=/home/chen/detect/build ;程序的启动目录

4.2第二种:写在supervisor.conf里

还是把下面这句话写一下

[program:detect]
command=/home/chen/detect/build/detect   ;进程的启动命令(detect是一个可执行进程)
process_name=%(program_name)s  ;进程名字(这是默认的写法)
numprocs=1 ;启动几个该进程
autostart=true ;在supervisord启动时也自动启动
autorestart=true ;程序异常退出后自动重启
directory=/home/chen/detect/build ;程序的启动目录

只不过这时,把它填写在 /etc/supervisor/supervisor.conf文件的最后
两种方式都可以实现进程监管
无论是单独写成.conf,还是写到supervisor.conf中,别忘了保存

二. Supervisor的组成

Supervisor主要由以下两部分组成:

1.supervisord

​     当我们启动Supervisor时,首先会有一个supervisord进程,称为父进程,它所管理的进程是它的子进程。supervisord进程负责统一管理这些子进程的启动、重启、停止。
使用命令可以查看是否已经有supervisor在跑了

ps -A | grep supervisord

可以通过命令来杀死supervisord,相当于关闭supervisord

sudo kill [supervisord进程id]

使用如下命令,重新启动supervisor并更新supervisor的配置

sudo supervisord -c /etc/supervisor/supervisor.conf

如果你在使用上述命令时报错,错误内容是

Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord

意思就是电脑上已经有supervisor在跑了,你按照上述方法杀掉它再重新启动就是了
在这里插入图片描述

2.supervisorctl

​     一个命令行管理工具,输入某些命令,如:start、stop、restart等,就可以对指定的进程进行相应的操作了,极其简单。
查看当前运行的进程列表:

sudo supervisorctl status

停止/开始/重启某一个进程

sudo supervisorctl stop/start/restart [进程名字]

​使用supervisorctl tail program_name stderr命令查看错误信息

sudo supervisorctl tail [进程名字] stderr

参考博客:

  1. Supervisor进程管理并设置为开机自动启动.
  2. supervisor安装配置与使用.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值