python 实用案例 supervisord管理进程详解
Supervisor是由python语言编写,基于linux操作系统的一款服务器管理工具,用以监控服务器的运行,发现问题能立即自动预警及自动重启等功能。
Supervisor类似于monit,
monit和supervisor的一个比较大的差异是supervisor管理的进程必须由supervisor来启动,
monit可以管理已经在运行的程序;
supervisor还要求管理的程序是非daemon程序,supervisord会帮你把它转成daemon程序,
因此如果用supervisor来管理nginx的话,必须在nginx的配置文件里添加一行设置daemon off让nginx以非daemon方式启动。
一、Supervisor的组成
1. supervisord
The server piece of supervisor is named supervisord.
It is responsible for starting child programs at its own invocation,
responding to commands from clients, restarting crashed or exited subprocesseses,
logging its subprocess stdout and stderr output, and generating and handling “events”
corresponding to points in subprocess lifetimes.
The server process uses a configuration file. This is typically located in /etc/supervisord.conf.
This configuration file is an “Windows-INI” style config file.
It is important to keep this file secure via proper filesystem permissions
because it may contain unencrypted usernames and passwords.
2. supervisorctl
The command-line client piece of the supervisor is named supervisorctl.
It provides a shell-like interface to the features provided by supervisord. From supervisorctl,
a user can connect to different supervisord processes, get status on the subprocesses controlled by,
stop and start subprocesses of, and get lists of running processes of a supervisord.
The command-line client talks to the server across a UNIX domain socket or an internet (TCP) socket.
The server can assert that the user of a client should present authentication credentials before it allows him to perform commands.
The client process typically uses the same configuration file as the server but any configuration file with a [supervisorctl]
section in it will work.
3. Web Server
A (sparse) web user interface with functionality comparable to supervisorctl may be accessed via a browser
if you start supervisord against an internet socket.
Visit the server URL (e.g. http://localhost:9001/) to view and control process status through
the web interface after activating the configuration file’s [inet_http_server] section.
4. XML-RPC Interface
The same HTTP server which serves the web UI serves up an XML-RPC interface that can be used to interrogate
and control supervisor and the programs it runs. See XML-RPC API Documentation.
Platform Requirements
二、Supervisor安装
首先必须安装好python环境,linux自带python,但建议安装2.7.0以上的版本。
Supervisor可以通过
$ sudo easy_install supervisor
安装。安装成功后显示finished, 可以再次进入python环境,
输入"import supervisor", 如果没有提示错误,则表示安装成功。
当然也可以通过Supervisor官网下载后setup.py install安装。
出现错误提示:
Installed /usr/local/python2.7.3/lib/python2.7/site-packages/supervisor-4.0.0_dev-py2.7.egg
Processing dependencies for supervisor==4.0.0-dev
Searching for meld3>=1.0.0
Reading https://pypi.python.org/simple/meld3/
Download error on https://pypi.python.org/simple/meld3/: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed -- Some packages may not be found!
Couldn't find index page for 'meld3' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
Download error on https://pypi.python.org/simple/: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed -- Some packages may not be found!
No local packages or download links found for meld3>=1.0.0
error: Could not find suitable distribution for Requirement.parse('meld3>=1.0.0')
解决办法:
上网查询了问题原因: 是curl的证书太老了需要下载最新的证书:
下载最新的证书文件 、
$ wget http://curl.haxx.se/ca/cacert.pem
更名为ca-bundle.crt放置到默认目录
$ mv cacert.pem ca-bundle.crt
$ mv ca-bundle.crt /etc/pki/tls/certs
下载并安装好证书后, 还是出现上述的问题,
根据证书过期联想到时间, 输入date命令查看时间, 原来是时间太小了,
用date -s 修改时间后,就可以正常的easy_install了。
三、Supervisor配置
接下来是对supervisor配置,首先要生成配置文件,在shell终端输入:
$ echo_supervisord_conf > /etc/supervisord.conf
可以通过文本编辑器修改这个文件,
$ vim /etc/supervisord.conf
下面是一个示例的配置文件:
;/etc/supervisord.conf
[unix_http_server]
file = /var/run/supervisor.sock
chmod = 0777
chown= root:root
[inet_http_server]
# Web管理界面设定
port=9001
;username = admin
;password = yourpassword
[supervisorctl]
; 必须和'unix_http_server'里面的设定匹配
serverurl = unix:///var/run/supervisord.sock
[supervisord]
logfile=/var/log/supervisord/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=true ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
user=root ; (default is current user, required if root)
childlogdir=/var/log/supervisord/ ; ('AUTO' child log dir, default $TEMP)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
;管理的单个进程的配置,可以添加多个program
[program:tail]
command=tail -f /etc/supervisord.conf
autostart = true
startsecs = 5
user = felinx
redirect_stderr = true
; 对这个program的log的配置,上面的logfile_maxbytes是supervisord本身的log配置
stdout_logfile_maxbytes = 20MB
stdoiut_logfile_backups = 20
stdout_logfile = /var/log/supervisord/chatdemo.log
[program:app]
command=python app.py --port=61000
directory=/opt/test/supervisor ; 先进入到这个目录,再执行command, 对于程序环境要在当前目录的程序很有用
autostart=true ; start at supervisord start (default: true)
autorestart=unexpected ; whether/when to restart (default: unexpected)
startsecs=1 ; number of secs prog must stay running (def. 1)
user=root
; 配置一组进程,对于类似的program可以通过这种方式添加,避免手工一个个添加
[program:groupworker]
command=python /home/felinx/demos/groupworker/worker.py
numprocs=24
process_name=%(program_name)s_%(process_num)02d
autostart = true
startsecs = 5
user = felinx
redirect_stderr = true
stdout_logfile = /var/log/supervisord/groupworker.log
;(更多配置说明请参考:http://supervisord.org/configuration.html)
编辑完成后保存退出.
使用命令启动supervisor:
$ supervisord
$ supervisorctl
用ps命令查看运行情况,应用现在已经自动运行了。
四、Supervisord管理
Supervisord安装完成后有两个可用的命令行supervisord和supervisorctl,
命令使用解释如下:
? supervisord, 初始启动Supervisord,启动、管理配置中设置的进程。
? supervisorctl stop tail, 停止某一个进程(programxxx),programxxx为[program:chatdemon]里配置的值,这个示例就是chatdemon。
? supervisorctl start tail, 启动某个进程
? supervisorctl restart tail,重启某个进程
? supervisorctl stop tail: ,重启所有属于名为groupworker这个分组的进程(start,restart同理)
? supervisorctl stop all, 停止全部进程,注:start、restart、stop都不会载入最新的配置文件。
? supervisorctl reload, 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程。
? supervisorctl update, 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。
注意:显示用stop停止掉的进程,用reload或者update都不会自动重启。
五、页面管理
supervisor自带有Web Server, 可以通过页面来管理进程,
前提是开启配置文件中的[inet_http_server]项。
如果服务器是单网卡,可以修改如下:
[inet_http_server] ; inet (TCP) server disabled by default
port=127.0.0.1:51000 ; (ip_address:port specifier, *:port for all iface)
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
如果是多网卡,则需要指定一张网卡:
[inet_http_server] ; inet (TCP) server disabled by default
port=192.168.2.13:51000 ; (ip_address:port specifier, *:port for all iface)
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
在浏览器地址栏中输入:
http://192.168.2.13:51000
就可以进行页面化的管理了。
python 实用案例 supervisord管理进程详解的更多相关文章
- supervisord管理进程详解
supervisord管理进程详解 supervisor配置详解(转) 官网 Linux后台进程管理利器:supervisor supervisor使用详解
- Python Tkinter Grid布局管理器详解
Grid(网格)布局管理器会将控件放置到一个二维的表格里.主控件被分割成一系列的行和列,表格中的每个单元(cell)都可以放置一个控件. 注意:不要试图在一个主窗口中混合使用pack和grid (1) ...
- Python编程之子进程管理(subprocess)详解
引言 在写程序时,我们无法避免需要运行外部程序,相较于功能比较简单的os.system(),更加倾向于使用subprocess模块来执行外部程序. 模块介绍 subprocess.run() 使用su ...
- 云计算:Linux运维核心管理命令详解
云计算:Linux运维核心管理命令详解 想做好运维工作,人先要学会勤快: 居安而思危,勤记而补拙,方可不断提高: 别人资料不论你用着再如何爽那也是别人的: 自己总结东西是你自身特有的一种思想与理念的展 ...
- PHP 进程详解
.note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...
- windows进程详解
1:系统必要进程system process 进程文件: [system process] or [system process]进程名称: Windows内存处理系统进程描述: Windows ...
- Linux学习之守护进程详解
Linux系统守护进程详解 ---转自:http://yuanbin.blog ...
- python 3.x 爬虫基础---Urllib详解
python 3.x 爬虫基础 python 3.x 爬虫基础---http headers详解 python 3.x 爬虫基础---Urllib详解 前言 爬虫也了解了一段时间了希望在半个月的时间内 ...
- Supervisord管理进程实践
今天凑空研究了下Supervisord,这是一款linux进程管理工具,使用python开发,主要用于在后台维护进程(类似master守护进程),可以实现监控进程的状态.自动重启进程等操作,便于一些服 ...
随机推荐
- zookeeper curator选主(Leader)
在分布式系统设计中,选主是一个常见的场景.选主是一个这样的过程,通过选主,主节点被选择出来控制其他节点或者是分配任务. 选主算法要满足的几个特征: 1)各个节点均衡的获得成为主节点的权利,一旦主节点被 ...
- 第九章 MySQL中LIMIT和NOT IN案例
第九章 MySQL中LIMIT和NOT IN案例 一.案例的项目 1.创建数据库语句: #创建数据库 CREATE DATABASE `schoolDB`; USE `schoolDB`; #创建学生 ...
- 51nod 1510 最小化序列 | DP 贪心
题目描述 现在有一个长度为n的数组A,另外还有一个整数k.数组下标从1开始. 现在你需要把数组的顺序重新排列一下使得下面这个的式子的值尽可能小. ∑|A[i]−A[i+k]| 特别的,你也可以不对数组 ...
- 使用acs-engine在Azure中国区部署kubernetes集群详解
转载请注明出处:http://www.cnblogs.com/wayneiscoming/p/7649642.html 1. acs-engine简介 ACS是微软在2015年12月推出的一项基于容器 ...
- LINUX 笔记-MOUNT
mount [-t vfstype] [-o options] device dir -o options: 主要用来描述设备或档案的挂接方式 1)loop:用来把一个文件当成硬盘分区挂上系统 2)r ...
- Spring bean 生命周期验证
一.从源码注释看bean生命周期 从JDK源码上看,BeanFactory实现类需要支持Bean的完整生命周期,完整的初始化方法及其标准顺序(格式:接口 方法)为: 1.BeanNameAware s ...
- 【ASP.NET MVC 学习笔记】- 17 Model验证
本文参考:http://www.cnblogs.com/willick/p/3434483.html 1.Model验证用于在实际项目中对用户提交的表单的信息进行验证,MVC对其提供了很好的支持. 2 ...
- Java基础-Random类(05)
随机数(Random) 作用:用于产生一个随机数 使用步骤(和Scanner类似) 导包import java.util.Random; 创建对象Random r = new Random(); 获取 ...
- Linux系统LVM基本使用
一.关于LVM的几个概念 1. LVM:逻辑卷管理器,是建立在物理存储设备上的一个抽象层,允许你生成逻辑存储卷, 与硬件相关的存储设置被其隐藏,你不用停止应用或卸载文件系统来调整卷大小 或迁移数据,可 ...
- LeetCode 277. Find the Celebrity (找到明星)$
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...