小标
2018-12-21
来源 :
阅读 1602
评论 0
摘要:本文主要向大家介绍了Linux运维知识之linux下使用supervisor监控应用程序,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。
本文主要向大家介绍了Linux运维知识之linux下使用supervisor监控应用程序,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。
1、应用场景
应用程序需要24小时不间断运行。这时可使用supervisor监控应用程序的进程。当发生应用程序内部错误退出、进程被杀死等情况时,自动重启应用程序。
2、supervisor
supervisor由python写成, 简单好用。官方网站 //supervisord.org,上面有详细的指南文档。
3、安装supervisor
1)安装 setuptools
$sudo apt-get install python-setuptools
2) 使用easy_install安装 supervisor
$sudo easy_install supervisor
安装完成后出现:
/usr/bin/supervisord --supervisor服务守护进程
/usr/bin/supervisorctl--supervisor服务控制程序
4、配置文件
以下命令可以将配置文件示例打印到控制台:
$ echo_supervisord_conf
若要生成配置文件,使用命令:
$ sudo echo_supervisord_conf > /etc/supervisord.conf
或者在当前目录生成:
$ echo_supervisord_conf > supervisord.conf
Notice: 如何停止子进程
场景:如果supervisord.conf中配置的command是执行一个bash,而bash里执行java,那么当使用supervisorctl stop [programname]停止程序时,只有上层进程被停止,而java进程没有被停止。
解决办法:
在配置文件中设置:
stopasgroup=true
killasgroup=true
5、配置要运行的程序
在上面生成的配置文件后,追加:
[program:helloworld]
command=./helloworld ;执行命令
process_name=%(program_name)s
autostart=true ; 程序是否随supervisor启动而启动
autorestart=true ;程序停止时,是否自动重启
startsecs=10
6、启动supervisor
$ supervisord -c supervisord.conf
7、使用supervisorctl管理程序
1)开启/停止某个程序
# supervisorctl [start | stop] [program名称] //在supervisord.conf中定义的
2)查看进程状态
$supervisorctl status
8、通过web方式查看状态
配置文件中配置:
[inet_http_server] ; inet (TCP) server disabled by default
port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
那么可以从浏览器通过访问 localhost:9001来查看进程状态
9、添加supervisord为Linux系统服务,开机自动启动
1)将supervisord.conf拷贝到 /etc 目录下
2)启动脚本 supervisord.sh (centos/redhat)
#!/bin/sh
#
# /etc/rc.d/init.d/supervisord
#
# Supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord
# Source init functions
. /etc/rc.d/init.d/functions
prog="supervisord"
prog_bin="/usr/local/bin/supervisord"
PIDFILE="/tmp/supervisord.pid"
start()
{
echo -n $"Starting $prog: "
# Source init functions
. /etc/rc.d/init.d/functions
prog="supervisord"
prog_bin="/usr/local/bin/supervisord"
PIDFILE="/tmp/supervisord.pid"start()
{
echo -n $"Starting $prog: "
daemon $prog_bin --pidfile $PIDFILE
[ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog startup"
echo
}
stop()
{
echo -n $"Shutting down $prog: "
[ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown"
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $prog
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
;;
esac
3)添加为系统服务
# mv supervisord.sh /etc/init.d/supervisord
# chkconfig --add supervisord
# chkconfig --level 345 supervisord on
4) 开启/停止服务
# service supervisord [start | stop]
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注系统运维Linux频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

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