Supervisor使用教程
linux kill 掉所有匹配到名字的进程
如,要 kill 掉 swoole 相关的进程
ps aux | grep swoole | awk ‘{print $2}’ | xargs kill -9
ps 列出所有进程,
参数:
a - 显示现行终端机下的所有进程,包括其他用户的进程;
u - 以用户为主的进程状态 ;
x - 通常与 a 这个参数一起使用,可列出较完整信息。
grep 过滤掉不包含 “swoole” 的行
awk ‘{print $2}’ 获取进程 ID (PID, Process Identification),我们想 kill 掉某一个进程的时候需要通过 PID 指定特定进程
xargs 将标准输入数据转换成命令行参数,xargs能够处理管道或者stdin并将其转换成特定命令的命令参数。
也就是将管道传递过来的每一个 PID 作为 kill -9 的参数
<div><p>在项目中,经常有脚本需要常驻运行的需求。以PHP脚本为例,最简单的方式是:</p><pre>$ nohup php cli.php &</pre><p>这样能保证当前终端被关闭或者按CRTL+C后,脚本仍在后台运行。但是没法保证脚本异常后自动重启等。</p><p>Supervisor 是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。</p><p>官网介绍:<a href="http://supervisord.org/">http://supervisord.org/</a></p><p>本文所用环境:</p><ul class="list-paddingleft-2"><li><p>CentOS release 6.8 (Final)</p></li><li><p>Python 2.6.6</p></li><li><p>pip 7.1.0 from /usr/lib/python2.6/site-packages (python 2.6)</p></li><li><p>supervisor 3.3.4</p></li></ul><h2>安装</h2><h3>平台要求</h3><p>引自官网(<a href="http://supervisord.org/introduction.html#platform-requirements">http://supervisord.org/introduction.html#platform-requirements</a>):</p><blockquote><p>Supervisor已经过测试,可以在Linux(Ubuntu 9.10),Mac OS X(10.4 / 10.5 / 10.6)和Solaris(10 for Intel)和FreeBSD 6.1上运行。它可能在大多数UNIX系统上都能正常工作。在任何版本的Windows下,Supervisor 都不会运行。Supervisor 可以使用<code>Python 2.4</code>或更高版本,但不能在任何版本的<code>Python 3</code>下使用。</p></blockquote><p>我使用的环境:</p><pre>$ python -V
Python 2.6.6
安装
安装方法有:
1、easy_install 安装(需安装有pip):
KaTeX parse error: Expected 'EOF', got '&' at position 1: &̲nbsp;easy_insta… pip install supervisor
3、Debian / Ubuntu可以直接通过apt安装:
KaTeX parse error: Expected 'EOF', got '&' at position 1: &̲nbsp;apt-get&nb… mkdir /etc/supervisor KaTeX parse error: Expected 'EOF', got '&' at position 1: &̲nbsp;echo_super… supervisord
即可运行。
supervisor 默认在以下路径查找配置文件:/usr/etc/supervisord.conf, /usr/supervisord.conf, supervisord.conf, etc/supervisord.conf, /etc/supervisord.conf, /etc/supervisor/supervisord.conf
如需指定主配置文件,则需要使用-c
参数:
KaTeX parse error: Expected 'EOF', got '&' at position 1: &̲nbsp;supervisor… supervisord -v 3.3.4
然后查看supervisor的状态:
KaTeX parse error: Expected 'EOF', got '&' at position 1: &̲nbsp;supervisor… /etc/init.d/supervisor start
运行。
使用示例
我们以简单的 /tmp/echo_time.sh
为例:
#/bin/bashwhile true; doecho date +%Y-%m-%d,%H:%m:%s
sleep 2done
在/etc/supervisor/conf.d/
新增子进程配置文件 echo_time.conf
:
[program:echo_time]command=sh /tmp/echo_time.shpriority=999 ; the relative start priority (default 999)autostart=true ; start at supervisord start (default: true)autorestart=true ; retstart at unexpected quit (default: true)startsecs=10 ; number of secs prog must stay running (def. 10)startretries=3 ; max # of serial start failures (default 3)exitcodes=0,2 ; ‘expected’ exit codes for process (default 0,2)stopsignal=QUIT ; signal used to kill process (default TERM)stopwaitsecs=10 ; max num secs to wait before SIGKILL (default 10)user=root ; setuid to this UNIX account to run the programlog_stdout=truelog_stderr=true ; if true, log program stderr (def false)logfile=/tmp/echo_time.loglogfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)logfile_backups=10 ; # of logfile backups (default 10)stdout_logfile_maxbytes=20MB ; stdout 日志文件大小,默认 50MBstdout_logfile_backups=20 ; stdout 日志文件备份数stdout_logfile=/tmp/echo_time.stdout.log
然后启动程序:
$ supervisorctl reread KaTeX parse error: Expected 'EOF', got '&' at position 1: &̲nbsp;supervisor… tail -f /tmp/echo_time.stdout.log 2018-12-22,14:12:1545459550 2018-12-22,14:12:1545459552 2018-12-22,14:12:1545459554
也可以使用supervisorctl status
查看子进程运行情况:
$ supervisorctl status echo_time RUNNING pid 28206, uptime 0:00:11
配置文件
主配置
主配置文件名: supervisord.conf
可以通过运行echo_supervisord_conf
获得。这个配置文件一般情况下不需要更改,除了最后的[include]
部分,其余保持默认即可。
[unix_http_server]file=/tmp/supervisor.sock ; the path to the socket file;chmod=0700 ; socket file mode (default 0700) ;chown=nobody:nogroup ; socket file uid:gid owner ;username=user ; default is no username (open server) ;password=123 ; default is no password (open server)
;[inet_http_server] ; 配置web后台
;port=127.0.0.1:9001 ; 指定ip_address:port, 使用 *:port 监听所有 IP
;username=user ; 默认没有用户名 (open server)
;password=123 ; 默认没有密码 (open server)
[supervisord]
logfile=/tmp/supervisord.log ; 日志文件; 默认 KaTeX parse error: Expected 'EOF', got '&' at position 42: …e_maxbytes=50MB&̲nbsp; &nbs…TEMP
;environment=KEY=“value” ; key value pairs to add to environment
;strip_ansi=false ; strip ansi escape codes in logs; def. false; The rpcinterface:supervisor 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:x] sections.
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
; 配置 supervisorctl
; configure it match the settings in either the unix_http_server
; or inet_http_server section.
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris ; should be same as in [_http_server] if set
;password=123 ; should be same as in [_http_server] if set
;prompt=mysupervisor ; cmd line prompt (default “supervisor”)
;history_file=~/.sc_history ; use readline history if available
; 下面是子进程配置文件示例
; Create one or more ‘real’ program: sections to be able to control them under
; supervisor.
;[program:theprogramname]
;command=/bin/cat ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=999 ; the relative start priority (default 999)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2 ; ‘expected’ exit codes used with autorestart (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10)
;stdout_capture_maxbytes=1MB ; number of bytes in ‘capturemode’ (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in ‘capturemode’ (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A=“1”,B=“2” ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
; The sample eventlistener section below shows all possible eventlistener
; subsection values. Create one or more ‘real’ eventlistener: sections to be
; able to handle event notifications sent by supervisord.
;[eventlistener:theeventlistenername]
;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;events=EVENT ; event notif. types to subscribe to (req’d)
;buffer_size=10 ; event buffer queue size (default 10)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=-1 ; the relative start priority (default -1)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; autorestart if exited after running (def: unexpected)
;exitcodes=0,2 ; ‘expected’ exit codes used with autorestart (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A=“1”,B=“2” ; process environment additions
;serverurl=AUTO ; override serverurl computation (childutils)
; The sample group section below shows all possible group values. Create one
; or more ‘real’ group: sections to create “heterogeneous” process groups.
;[group:thegroupname]
;programs=progname1,progname2 ; each refers to ‘x’ in [program:x] definitions
;priority=999 ; the relative start priority (default 999)
; 配置include files
; 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]
; .ini和.conf都支持
files = relative/directory/*.ini
子进程配置文件
一般放在:/etc/supervisor/conf.d/
目录 。一个脚本对应一个配置文件。
配置说明:
;为必须填写项;[program:应用名称][program:cat];*命令路径,如果使用python启动的程序应该为 python /home/test.py, ;不建议放入/home/user/, 对于非user用户一般情况下是不能访问command=/bin/cat;当numprocs为1时,process_name=%(program_name)s;当numprocs>=2时,%(program_name)s_%(process_num)02dprocess_name=%(program_name)s;进程数量numprocs=1;执行目录,若有/home/supervisor_test/test1.py;将directory设置成/home/supervisor_test;则command只需设置成python test1.py;否则command必须设置成绝对执行目录directory=/tmp;掩码:— -w- -w-, 转换后rwx r-x w-xumask=022;优先级,值越高,最后启动,最先被关闭,默认值999priority=999;如果是true,当supervisor启动时,程序将会自动启动autostart=true;*自动重启autorestart=true;启动延时执行,默认1秒startsecs=10;启动尝试次数,默认3次startretries=3;当退出码是0,2时,执行重启,默认值0,2exitcodes=0,2;停止信号,默认TERM;中断:INT(类似于Ctrl+C)(kill -INT pid),退出后会将写文件或日志(推荐);终止:TERM(kill -TERM pid);挂起:HUP(kill -HUP pid),注意与Ctrl+Z/kill -stop pid不同;从容停止:QUIT(kill -QUIT pid);KILL, USR1, USR2其他见命令(kill -l),说明1stopsignal=TERMstopwaitsecs=10;*以root用户执行user=root;重定向redirect_stderr=falsestdout_logfile=/a/pathstdout_logfile_maxbytes=1MBstdout_logfile_backups=10stdout_capture_maxbytes=1MBstderr_logfile=/a/pathstderr_logfile_maxbytes=1MBstderr_logfile_backups=10stderr_capture_maxbytes=1MB;环境变量设置environment=A=“1”,B="2"serverurl=AUTO
简化模板
[program:echo_time]command=sh /tmp/echo_time.shautostart=trueautorestart=truestartsecs=10startretries=3 exitcodes=0,2stopsignal=QUITstopwaitsecs=10user=rootlog_stdout=truelog_stderr=truelogfile=/tmp/echo_time.loglogfile_maxbytes=1MBlogfile_backups=10 stdout_logfile_maxbytes=20MB stdout_logfile_backups=20 stdout_logfile=/tmp/echo_time.stdout.log
命令行程序
supervisord
supervisord 是主进程。
通过supervisord -h
可以查看帮助说明。示例:
-c/–configuration FILENAME ;指定配置文件-n/–nodaemon ;运行在前台(调试用)-v/–version ;打印版本信息-u/–user USER ;以指定用户(或用户ID)运行-m/–umask UMASK ;指定子进程的umask,默认是022-l/–logfile FILENAME ;指定日志文件-e/–loglevel LEVEL ;指定日志级别
supervisorctl
supervisorctl 是客户端程序,用于向supervisord发起命令。
通过supervisorctl -h
可以查看帮助说明。我们主要关心的是其action
命令:
$ supervisorctl helpdefault 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
这些命令对于控制子进程非常重要。示例:
reread ;重新加载配置文件 update ;将配置文件里新增的子进程加入进程组,如果设置了autostart=true则会启动新新增的子进程 status ;查看所有进程状态 status <name> ;查看指定进程状态 start all; 启动所有子进程 start <name>; 启动指定子进程 restart all; 重启所有子进程 restart <name>; 重启指定子进程 stop all; 停止所有子进程 stop <name>; 停止指定子进程 reload ;重启supervisord add <name>; 添加子进程到进程组 reomve <name>; 从进程组移除子进程,需要先stop。注意:移除后,需要使用reread和update才能重新运行该进程
supervisord 有进程组(process group)的概念:只有子进程在进程组,才能被运行。
supervisorctl
也支持交互式命令行:
KaTeX parse error: Expected 'EOF', got '&' at position 1: &̲nbsp;supervisor… supervisorctl reload
浏览器访问:http://myip:9001 ,输入用户名、密码后,即可看到web页面:
注意:如果修改配置文件时,
[inet_http_server]
这一行被注释,会导致不仅web需要认证,命令行使用supervisorctl
也需要认证,这时候就需要在交互式命令行里输入用户名、密码才能进行下一步的操作。
其它问题
1、Centos6 docker环境没有pip
解决方案:需要先安装扩展源EPEL。
EPEL(http://fedoraproject.org/wiki/EPEL) 是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目。
首先安装epel扩展源:
KaTeX parse error: Expected 'EOF', got '&' at position 1: &̲nbsp;yum -… yum -y install python-pip
查看版本:
KaTeX parse error: Expected 'EOF', got '&' at position 1: &̲nbsp;pip -… supervisor -V
出现:
Traceback (most recent call last): File “/usr/bin/echo_supervisord_conf”, line 5, in <module> from pkg_resources import load_entry_point File “/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/pkg_resources.py”, line 2603, in <module> File “/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/pkg_resources.py”, line 666, in require File “/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/pkg_resources.py”, line 565, in resolve pkg_resources.DistributionNotFound: meld3>=0.6.5
原因:pip安装的meld3不可用,手动安装。
wget https://pypi.python.org/packages/source/m/meld3/meld3-1.0.2.tar.gz tar -zxf meld3-1.0.2.tar.gzcd meld3-1.0.2 python setup.py install
感谢https://www.cnblogs.com/hubery/p/5653007.html 文章提供的方法!这个问题困扰了好久,github页面(https://github.com/Supervisor/meld3/issues/23)提供的解决方案都不可行,反而导致pip都不能用了。
3、因参考github里meld3解决方案导致pip不可用:
Traceback (most recent call last): File “/usr/bin/pip”, line 5, in <module> from pkg_resources import load_entry_point File “/usr/lib/python2.6/site-packages/pkg_resources/init.py”, line 957, in <module> class Environment: File “/usr/lib/python2.6/site-packages/pkg_resources/init.py”, line 961, in Environment self, search_path=None, platform=get_supported_platform(), File “/usr/lib/python2.6/site-packages/pkg_resources/init.py”, line 188, in get_supported_platformplat = get_build_platform() File “/usr/lib/python2.6/site-packages/pkg_resources/init.py”, line 391, in get_build_platform from sysconfig import get_platform ImportError: No module named sysconfig
解决方案:删除/site-packages
下面的包,重新安装python-setuptools:
rm -rf /usr/lib/python2.6/site-packages/pkg_resources* yum reinstall python-setuptools
参考:https://stackoverflow.com/questions/50742538/importerror-no-module-named-sysconfig-cant-get-pip-working
参考
1、Supervisor: A Process Control System — Supervisor 3.3.4 documentation
http://supervisord.org/index.html
2、进程管理supervisor的简单说明 - jyzhou - 博客园
https://www.cnblogs.com/zhoujinyi/p/6073705.html
3、DevOps
https://mp.weixin.qq.com/s/mqrkAEaGFKJy-4PQadJJ9w?
4、supervisor使用 - 回首郑板桥 - 博客园
https://www.cnblogs.com/hubery/p/5653007.html
原文出处:https://www.cnblogs.com/52fhy/p/10161253.html
PHP
若觉得本文不错,就分享一下吧!
Supervisor使用教程相关推荐
- python怎么打开程序管理器_Python 进程管理工具 Supervisor 使用教程
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,Python 进程管理工具 Supervisor 使用教程 Supervisor ...
- 【Python】supervisor 工具介绍
一 简介 Supervisor 是一款基于Python的进程管理工具,可以很方便的管理服务器上部署的应用程序.supervisor是C/S模型的程序,其server端是supervisord 服务 ...
- Python实现的进程管理神器——Supervisor
文章目录 常用命令 简介 安装 创建配置文件 开机自启 初试 Web 界面 配置文件 子进程配置模板 可用变量 supervisorctl 命令 Supervisor 组件 卸载 遇到的坑 参考文献 ...
- laradock 切换php版本,laradock 使用 php-worker 配置 supervisor
laradock 使用 php-worker 配置 supervisor laradock 使用 php-worker 配置 supervisor 导语: 因为项目使用了队列,需要执行命令:php t ...
- 001-supervisor
supervisor 使用教程(转) 原文地址:https://word.gw1770df.cc/2016-08-04/linux/supervisor-%E4%BD%BF%E7%94%A8%E6%9 ...
- 在laravel5 中使用queue队列
如何在laravel5 中使用queue队列 Laravel Queue是延迟处理应用程序中耗时任务的有效方法.此类任务的示例可能包括每当新用户在您的应用程序中注册或通过社交媒体分享帖子时发送验证电子 ...
- mysql中explain命令
原文链接:https://blog.csdn.net/jiadajing267/article/details/81269067 其他文章: https://www.cnblogs.com/tufuj ...
- Erlang/OTP设计原则(文档翻译)
http://erlang.org/doc/design_principles/des_princ.html 图和代码皆源自以上链接中Erlang官方文档,翻译时的版本为20.1. 这个设计原则,其实 ...
- 借助Docker,在win10下编码,一键在Linux下测试
此前在公司实习时,日常的开发.工作按规定都必须使用Windows操作系统,但是项目实际的测试.上线环境都是基于Linux的,所以每次只能在本地编写某一功能的代码后通过"跳板机"将项 ...
最新文章
- java通过ip获取网卡MAC地址
- BP人工神经网络的介绍与实现
- grub光盘修复,硬盘修复
- 功能性农业实用技术 谋定·农业大健康-李喜贵:粤黔东西协作
- 三菱gxworks3安装失败_三菱电梯nexway故障表
- 游戏开发-从零开始 002
- webapi 设置参数可为空_Web API系列(二):灵活多样的路由配置
- β射线与哪些物质可产生较高的韧致辐射_什么是α射线、β射线、γ射线
- 死磕 java同步系列之JMM(Java Memory Model)
- Thinkpad x230 win7/xp 双系统安装全过程
- 【智慧医疗】破解医疗数据孤岛,实现信息共享
- Vue实例与组件实例
- 离线ROS API文档(Zeal或Dash)
- poj-1069(三角形和六边形)(转)
- 线性代数学习笔记——第十九讲——克拉默法则
- 大侠周鸿祎——腾讯,你丫动手吧!
- SpringCloud(7) LCN分布式事务框架入门
- 在 Azure 上部署 Kubernetes 集群
- yjv是电缆还是电线_YJV电缆与YJY电缆哪个价格高,两者的区别是什么?
- 基于JAVA的网上订餐外卖系统(Java+MySQL)
热门文章
- mysql8.0怎么导入数据_MySQL8.0导入数据
- mysql一次读取500条数据_mysql批量插入500条数据
- mysql6支持connect by_mysql 实现oracle start with connect by递归
- 三、数据分析前,打下数据处理基础(下)
- 五十、简单的斗鱼分析案例
- ACL 2021|CHASE: 首个跨领域多轮Text2SQL中文数据集
- 重磅推荐 | 11个名企NLP项目,硅谷科学家帮你转型
- 直播实录 | 哈工大博士生周青宇:从编码器与解码器端改进生成式句子摘要
- GAN做图像翻译的一点总结
- Java中Map集合类的用法(HashMap)