Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15996
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-11 21:55
个人简介

以战养战。通过做项目的形式去学习。更多的POC的形式去把思路DEMO化。

文章分类

全部博文(7)

文章存档

2016年(1)

2015年(6)

我的朋友

分类: LINUX

2015-11-09 22:21:50

Supervisor是一款Linux下的进程管理软件。最主要的两个功能是:

  • 将非daemon程序变成deamon方式运行
  • 对程序进行监控,当程序退出时,可以自动拉起程序

PS:好好研究一下supervisor的实现原理。监听一个sock文件然后可以管理子进程里面的启动,状态,关掉。这种机制非常类似于watchdog的机制。

安装

Supervisor是基于python开发的。安装Supervisor前,需要先安装python。Supervisor可以通过pip或者easy_install安装。

  • 通过pip安装

    pip install supervisor
  • 通过easy_install安装

    easy_install supervisor

基本使用

  • 生成配置文件

Supervisor没有默认配置文件,需要手工执行命令生成配置文件:

启动Supervisor:

supervisord -c /etc/supervisord.conf

停止Supervisor(子进程也会被停止)

supervisorctl shutdown

查看Supervisor是否已经启动:

[root@aaa blog.cheyo.net]# ps -ef | grep supervisor | grep -v grep
root      1170     1  0 18:57 ?        00:00:00 /usr/bin/python /usr/bin/supervisord
[root@aaa blog.cheyo.net]#

查看业务进程是否已经被拉起:

[root@aaa blog.cheyo.net]# supervisorctl status
check12306                       RUNNING   pid 1230, uptime 0:04:39
[root@aaa blog.cheyo.net]#

维护命令

Supervisor可通过维护命令supervisorctl管理或通过web管理界面管理。维护命令supervisorctl有两种用法。一种是命令式,一种是交互式。

  • 命令式
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# 查询各进程运行状态 supervisorctl status # 启、停、重启业务进程,check12306为进程名,即[program:check12306]里配置的值 
supervisorctl start check12306
supervisorctl stop check12306
supervisorctl restart check12306 #重启所有属于名为groupworker这个分组的进程 
supervisorctl start groupworker
supervisorctl stop groupworker
supervisorctl restart groupworker #启、停、重启全部进程(不会载入最新的配置文件) 
supervisorctl start all
supervisorctl stop all
supervisorctl restart all #重新加载配置文件.停止原有进程并按新的配置启动所有进程 
supervisorctl reload #根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。 
supervisorctl update #注意:显示用stop停止掉的进程,用reload或者update都不会自动重启 
  • 交互式
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[root@aaa blog.cheyo.net]# supervisorctl  
check12306                       RUNNING   pid 1256, uptime 0:01:47  
supervisor> stop check12306  
check12306: stopped  
supervisor> start check12306
check12306: started
supervisor> status
check12306                       RUNNING   pid 1258, uptime 0:00:04
supervisor> restart check12306
check12306: stopped
check12306: started
supervisor> status
check12306                       RUNNING   pid 1259, uptime 0:00:02
supervisor>
上面讲了一下supervisored及supervisorctl的使用技巧。现在遇到的一个场景是有一个服务需要用它进行管理。那写restart的逻辑我们就得先判断supervisored进程是不是在了,然后再用ctl进行管理服务
那我们的脚本应该怎么写呢?

点击(此处)折叠或打开

  1. # source function library
  2. . /etc/rc.d/init.d/functions

  3. # source system settings
  4. [ -e /etc/sysconfig/supervisord ] && . /etc/sysconfig/supervisord

  5. RETVAL=0

  6. start() {
  7.     echo "Starting supervisord: "
  8.     if [ -e $PIDFILE ]; then
  9.         echo "ALREADY STARTED"
  10.         return 1
  11.     fi

  12.     # start supervisord with options from sysconfig (stuff like -c)
  13.     /usr/bin/supervisord $OPTIONS
  14.     
  15.     # show initial startup status
  16.     /usr/bin/supervisorctl $OPTIONS status
  17.     
  18.     # only create the subsyslock if we created the PIDFILE
  19.     [ -e $PIDFILE ] && touch /var/lock/subsys/supervisord
  20. }

  21. stop() {
  22.     echo -n "Stopping supervisord: "
  23.     /usr/bin/supervisorctl $OPTIONS shutdown
  24.     if [ -n "$WAIT_FOR_SUBPROCESSES" ]; then
  25.         echo "Waiting roughly 60 seconds for $PIDFILE to be removed after child processes exit"
  26.         for sleep in 2 2 2 2 4 4 4 4 8 8 8 8 last; do
  27.             if [ ! -e $PIDFILE ] ; then
  28.                 echo "Supervisord exited as expected in under $total_sleep seconds"
  29.                 break
  30.             else
  31.                 if [[ $sleep -eq "last" ]] ; then
  32.                     echo "Supervisord still working on shutting down. We've waited roughly 60 seconds, we'll let it do its thing from here"
  33.                     return 1
  34.                 else
  35.                     sleep $sleep
  36.                     total_sleep=$(( $total_sleep + $sleep ))
  37.                 fi

  38.             fi
  39.         done
  40.     fi

  41.     # always remove the subsys. we might have waited a while, but just remove it at this point.
  42.     rm -f /var/lock/subsys/supervisord
  43. }

  44. restart() {
  45.         stop
  46.         start
  47. }

  48. case "$1" in
  49.     start)
  50.         start
  51.         RETVAL=$?
  52.         ;;
  53.     stop)
  54.         stop
  55.         RETVAL=$?
  56.         ;;
  57.     restart|force-reload)
  58.         restart
  59.         RETVAL=$?
  60.         ;;
  61.     reload)
  62.         /usr/bin/supervisorctl $OPTIONS reload
  63.         RETVAL=$?
  64.         ;;
  65.     condrestart)
  66.         [ -f /var/lock/subsys/supervisord ] && restart
  67.         RETVAL=$?
  68.         ;;
  69.     status)
  70.         /usr/bin/supervisorctl $OPTIONS status
  71.         RETVAL=$?
  72.         ;;
  73.     *)
  74.         echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
  75.         exit 1
  76. esac

  77. exit $RETVAL

这个是启动superviord的服务。需要进一步改成supervisorctl的方式进行处理




阅读(767) | 评论(0) | 转发(0) |
0

上一篇:运维产品总结

下一篇:如何去做好一个CMDB

给主人留下些什么吧!~~