supervisor process management tools of [Detailed]

 

Original link: https: //blog.csdn.net/weixin_42390791/article/details/88866237

First, the problem background
1, background
how to get rid of a terminal process, to obtain a relatively long life cycle?

2, the background (daemons), foreground process
  What is a daemon? Popular speak is the background ran the process, not because you closed the Terminal Services will follow stop until you turn off the power to the computer. When the process becomes a background process, the same terminal will be released, in which you can do other things, and will not interfere with your running services. Generally speaking background process can not capture the input and output services can still choose output terminal.
By the same token, understanding foreground process much easier, your foreground process is a service running, but can no longer be in the same terminal for other things, we can continue to do after you have stopped to existing services else, foreground process can capture input and output.

3, how to start a foreground process as a background process
been developed friends used Python Django will run through the project in their local environment. The following diagram, this service up and running, we often referred to as a foreground process, this time in the terminal, you can not do anything else, of course, to do so are generally used to view print debug information in the terminal, but on a remote server it? The situation is different now.

 

Here is a simple web application (bottle- lightweight frame, and interested friends can go to learn, pip install bottle can be installed) with Python bottle written
into the terminal

vim server.py
1
from bottle import route, run

@route ( "/")
DEF printStr ():
return "Hello World"

RUN (Host = "localhost", Port = 8090, Debug = True)

at the terminal, the application running up

$ python server.py

It appears as shown on the map, visit http: // localhost: 8090 /, the browser will appear "hello world", then, ctrl + c After the visit will be the error. The following shows how to start the process as a background process
background process (1) can still output in the terminal, on the command line plus the ampersand, still accessible

$ python server.py &

 


Can be seen that the output terminal is still captured
(2) using the binding & nohup command symbol, the command generates nohup.out file in the current directory, the file holds information of the terminal to print out the present, as shown in FIG.

$ nohup python server.py &

 


4, how to identify background processes
above fucks a pass becomes a background process? Do not worry, we look through the ps command

$ ps aux
1

TT ?? as a background process, that is not too foreground process, there is a foreground process is to have a clearly marked '' + "No.

5、存在的问题
​   上述启动为后台进程的方法,其实是存在很多问题的。
​   (1)从项目的角度看,一个项目往往不止起一个进程,还可能有其他的进程,那么,如何进行统一的进程管理呢?
​   (2)在进程运行的过程中,因为某种原因,挂掉了,如何做到不用人为干预自动重启进程呢?
​   supervisor完美解决上面的两个问题,当然可能还有更为强大的功能。

二、什么是supervisor
1、定义
​   Supervisor是用Python编写的目前只能在类unix操作系统中使用的一个进程管理工具。
​   注意,是一个管理工具,并不是库或包。

2、作用
​  Supervisor进程管理工具可以高效简单地对单个或者多个进程进行统一管理,如启动、重启、停止进程。更重要的作用是能在进程因为某种原因崩溃时,做到自动重启

3、Supervisor的组成
​   Supervisor主要由以下两部分组成:
​   (1)supervisord:
​     当我们启动Supervisor时,首先会有一个supervisord进程,称为父进程,它所管理的进程是它的子进程。supervisord进程负责统一管理这些子进程的启动、重启、停止。某种角度上看,有点NGINX的master与worker的感觉吧。
​   (2)supervisorctl:
​     一个命令行管理工具,输入某些命令,如:start、stop、restart等,就可以对指定的进程进行相应的操作了,极其简单。
​   如何简单地理解?
​     可以将supervisord理解为服务器,supervisorctl理解为客户端,输入的一些命令可以看做是客户端与服务器的交互过程。更牛逼的是Supervisor提供了web api在浏览器上就可以直接进行对进程的可视化管理。
​   接下来,在使用Supervisor进程管理工具前,先来看看一些配置项,当然安装好Supervisor之后,它的配置文件里已经有很详细的注释了,这里主要介绍子进程的配置文件的配置参数。下图就是三个子进程的配置文件,为什么要拿子进程的配置出来,形成一个独立的配置文件呢?当然是为了维护方便,就像很多人用NGINX一样,把配置文件按模块进行管理

 

​   子进程配置文件的示例
​   常用配置参数说明:

 

配置项 说明
directory 就是你项目所在的位置,supervisord会自动切换到这个目录
command 你跑项目的命令
user 你用什么身份起进程
autostart 当设置为true时,当supervisord启动时,该子进程就会自动启动
autorestart 当设置为true时,子进程因为某种原因挂掉,会自动进行重启
startsecs 该子进程启动多久后,才认为进程启动成功
startretries 子进程尝试情动的次数,默认为3
redirect_stderr 当设置为true时,子进程的标准错误输出重定向到supervisord后台的标准输出文件描述符
stdout_logfile 子进程标准输出存放的路径
stdout_logfile_maxbytes 标准输出文件达到多少后进行轮转
stdout_logfile_backups 标准输出日志的备份数量
priority 子进程启动的优先级,值越小启动越早
​   接下来,就是怎么用Supervisor的问题了

三、如何使用supervisor
1、安装Supervisor
​   两种方式进行安装

$ brew install supervisor #(本人机子是MAC,其他类unix操作系统的发行版本,自行使用相应的软件管理命令)
1
$ pip install supervisor
1
​ 注意:supervisor只运行在python2.4以上的版本,但是不支持Python3.X,如果你的机子没有相应的版本就Google一下解决办法吧
​   安装完成之后,我的是默认的安装路径

$ cd /usr/local/etc/

 

​   上图所示的supervisord.ini文件就是安装后产生的文件,supervisor.d是我自己自自建的文件夹,这个待会说。
​    linux操作系统的是安装路径

 

​   我们先来看看supervisord.ini是什么?

2、编辑配置文件
$ cat -n supervisord.ini
1
​   可以看到大概有148行,有9个配置选项

 

 

​   上图中画红色横线的地方要注意,有些人的supervisord.pid 以及 supervisor.sock 是放在 /tmp 目录下,这个目录是放临时文件的,容易丢失,强烈建议改目录放置,建议放在/var/run/下。
​   别看这supervisord.ini配置文件一大推东西,其实真正需要配的地方很少,本人就只是选用了第二个框的配置项,这里还提一下【include】配置项,跟NGNIX的配置文件如出一辙,意思是:我这里配置文件还包含了这个目录下的所有配置文件。还记得上本提到的supervisor.d目录吗?里面可以放置很多**.ini文件,而这些.ini**的文件其实很简单,仅仅包含了上图中的第六个配置项,这样就达到一个子进程一个配置文件,方便进行维护的目的。

$ ls supervisor.d/

 


$ vim server2.py

 

​  上图的红框的内容,其实就是主配置文件中的第六个框的配置项,只是把它抽出来,形成独立的配置文件,达到分而治之的目的。
  以上就配置好了两个子进程,接下来就是跑起来了

3、启动supercisord
$ supervisord -c /usr/loacl/etc/supervisord.ini #注意:要以配置文件的方式开启服务
1

​   上图的现象是因为我已经启动了supervisord进程,下面通过supervisorctl命令行工具进行进程管理

4、使用supervisorctl进行进程管理
​   进入supervisorctl交互界面,也要以带有配置文件的方式进入,这样才会跟踪到配置

$ supervisorctl -c /usr/local/etc/supervisord.ini

 


​   上图的现象是因为我把代码切换到了一个正在开发的分支上

supervisor> help #查看命令

 

​  于是乎,可以根据命令自由地对子进程进行管理。
  下表是一些常用的命令:

常用的命令 说明
status 查看当前管理的子进程
reload 当配置发生改变时,进行热部署
restart [program_name] 重启某个子进程
start [program_name] 启动某个子进程
5、可以通过web页面进行进程可视化进程管理
​   使用浏览器访问127.0.0.1:9001,进入管理可视化界面

 

 

Guess you like

Origin www.cnblogs.com/xingxia/p/python_supervisor.html