小标
2019-05-14
来源 :
阅读 2195
评论 0
摘要:本文主要向大家介绍了Linux运维知识之使用 Supervisor 管理服务器后台进程,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。
本文主要向大家介绍了Linux运维知识之使用 Supervisor 管理服务器后台进程,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。

Supervisor (//supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动、重启、关闭进程(不仅仅是 Python 进程)。除了对单个进程的控制,还可以同时启动、关闭多个进程,比如很不幸的服务器出问题导致所有应用程序都被杀死,此时可以用 supervisor 同时启动所有应用程序而不是一个一个地敲命令启动。
之所以使用 Supervisor,是因为服务器的 MongoDB 进程偶尔会 Crash,需要确保让它在挂掉后自动重启确保服务正常。
直接使用 pip 进行安装:
$ sudo pip install supervisor # 可能你会收到类似的报错:Supervisor requires Python 2.4 or later but does not work on any version of Python 3. You are using version 3.4.3 (default, Oct 28 2017, 20:59:04) # 可以手动安装新版 Supervisor,它支持 Python3: $ pip install git+https://github.com/Supervisor/supervisor # 设置环境变量: $ vim ~/.bash_profile 在后面补充: PATH=$PATH:$HOME/bin:/usr/local/python/bin $ source ~/.bash_profile
$ echo_supervisord_conf > /etc/supervisord.conf
打开配置文件:
$ vim /etc/supervisord.conf [unix_http_server] file=/tmp/supervisor.sock ; UNIX socket 文件,supervisorctl 会使用 ;chmod=0700 ; socket 文件的 mode,默认是 0700 ;chown=nobody:nogroup ; socket 文件的 owner,格式: uid:gid ;username=user ; default is no username (open server) ;password=123 ; default is no password (open server) ;[inet_http_server] ; HTTP 服务器,提供 web 管理界面 ;port=127.0.0.1:9001 ; Web 管理后台运行的 IP 和端口,如果开放到公网,需要注意安全性 ;username=user ; 登录管理后台的用户名 ;password=123 ; 登录管理后台的密码 [supervisord] logfile=/tmp/supervisord.log ; 日志文件,默认是 $CWD/supervisord.log logfile_maxbytes=50MB ; 日志文件大小,超出会 rotate,默认 50MB logfile_backups=10 ; 日志文件保留备份数量默认 10 loglevel=info ; 日志级别,默认 info,其它: debug,warn,trace pidfile=/tmp/supervisord.pid ; pid 文件 nodaemon=false ; 是否在前台启动,默认是 false,即以 daemon 的方式启动 minfds=1024 ; 可以打开的文件描述符的最小值,默认 1024 minprocs=200 ; 可以打开的进程数的最小值,默认 200 ; 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:///tmp/supervisor.sock ; 通过 UNIX socket 连接 supervisord,路径与 unix_http_server 部分的 file 一致 ;serverurl=//127.0.0.1:9001 ; 通过 HTTP 的方式连接 supervisord ;包含其他的配置文件 [include] files = relative/directory/*.ini ; 可以是 *.conf 或 *.ini
这里以添加 MongoDB 进程为例,首先修改 supervisord.conf:
$ vim supervisord.conf # 找到最后一行,并取消注释和添加: [include] files = /etc/supervisor/*.conf $ mkdir /etc/supervisor $ cd /etc/suervisor $ vim mongodb.conf # 填入以下内容: [program:mongodb] command = /usr/bin/mongod -port 27017 --dbpath /vr/lib/mongo autostart = true ; 在 supervisord 启动的时候也自动启动 startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了 autorestart = true ; 程序异常退出后自动重启 startretries = 3 ; 启动失败自动重试次数,默认是 3
Supervisor 有两个主要的组成部分:
supervisord,运行 Supervisor 时会启动一个进程 supervisord,它负责启动所管理的进程,并将所管理的进程作为自己的子进程来启动,而且可以在所管理的进程出现崩溃时自动重启。
supervisorctl,是命令行管理工具,可以用来执行 stop、start、restart 等命令,来对这些子进程进行管理。
$ supervisord -c /etc/supervisord.conf $ supervisorctl -c /etc/supervisord.conf status> mongodb RUNNING pid 2366, uptime 0:01:00
$ vim /etc/supervisord.conf # 取消注释和更改设置 [inet_http_server] ; HTTP 服务器,提供 web 管理界面 port=0.0.0.0:8080 ; Web 管理后台运行的 IP 和端口,如果开放到公网,需要注意安全性 username=user ; 登录管理后台的用户名 password=123 ; 登录管理后台的密码 [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=//0.0.0.0:8080 ; 通过 HTTP 的方式连接 supervisord
通过浏览器打开网站 url:8080,输入帐号密码,可以在网页中查看进程:
Linux 在启动的时候会执行 /etc/rc.local 里面的脚本,所以只要在这里添加执行命令就可以:
# 如果是 Centos 添加以下内容 /usr/bin/supervisord -c /etc/supervisord.conf # 以上内容需要添加在 exit 命令前,而且由于在执行 rc.local 脚本时,PATH 环境变量 # 未全部初始化,因此命令需要使用绝对路径。可以使用一下命令查看绝对路径: $ sudo find / -name supervisord > /usr/local/python/bin/supervisord 所以要改下路径: /usr/local/python/bin/supervisord -c /etc/supervisord.conf
在启动 supervisorctl 的时候可能会接受到 refuse connection 的报错,解决办法:
# 找到 supervisor.sock 的地址 $ find / -name supervisor.sock # unlink 掉它,*** 换成真实地址 $ unlink /***/supervisor.sock
还遇到了另外一个问题,在 supervisor 运行一段时间后,web 端会访问不了,在后台企图通过 supervisorctl -c /etc/supervisord.conf 登录,发现还是报 refuse connection 的错误,还有
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.
尝试将[supervisorctl] 里面的属性 serverurl 修改成 unix 前缀,如 unix:///tmp/supervisord.sock,过一段时间再做观察。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注系统运维Linux频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号