在linux服务需要可靠的运行,并且服务exit后,需要自动重新启动起来,还可以监控进程状态,supervisor就是个不错的选项,supervisor是用python开发的一个进程管理器。下面是安装配置过程。


   1.安装supervisor

        

        easy_install supervisor     SetupTool安装方法     

        apt-get install supervisor    Debian/Ubuntu安装方法

        yum install supervisor     RedHat/Centos安装方法

        


   2.生成默认配置文件

        echo_supervisord_conf > /etc/supervisord.conf



   3.修改/etc/supervisord.conf配置文件

        如需要访问web控制界面,inet_http_server区段修改为

        [inet_http_server]

        port=0.0.0.0:9001

        username=username ; 你的用户名

        password=password ; 你的密码


   4.修改启动脚本-将supervisord加入系统服务

        

        #!/bin/sh

        #

        # 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/init.d/functions

        RETVAL=0

        prog="supervisord"

        pidfile="/tmp/supervisord.pid"

        lockfile="/var/lock/subsys/supervisord"

        start()

        {

            echo -n $"Starting $prog: "

        daemon --pidfile $pidfile supervisord -c /etc/supervisord.conf

        RETVAL=$?

        echo

        [ $RETVAL -eq 0 ] && touch ${lockfile}

        }

        stop()

        {

            echo -n $"Shutting down $prog: "

        killproc -p ${pidfile} /usr/bin/supervisord

        RETVAL=$?

        echo

        if [ $RETVAL -eq 0 ] ; then

         rm -f ${lockfile} ${pidfile}

        fi

        }

        case "$1" in

        start)

                start ;;

        stop)      stop ;;

        status)

                status $prog ;;

        restart)

                stop

                start ;;

        *)

                echo "Usage: $0 {start|stop|restart|status}" ;;

        esac

     


    5.赋予权限加到开机启动

        chmod +x /etc/init.d/supervisord

        chkconfig supervisord on

        service supervisord start



       目前所有的supervisor基本的都完成,那么我现在用httpd来受被管理测试是否可以启动         并访问和exit后会不会自启动。



     6.修改/etc/supervisord.conf在最后添加以下内容

        [program:apache]

        command=/usr/local/apache/bin/httpd -D FOREGROUND   

        autostart=true

        autorestart=true

        startsecs=3

        user=root


    7.重新加载配置文件是否80端口可以访问

        service  supervisord restart

        

        日志显示正常:

              CRIT Supervisor running as root (no user in config file)

              INFO RPC interface 'supervisor' initialized

             INFO RPC interface 'supervisor' initialized

             CRIT Server 'unix_http_server' running without any HTTP authentication checking

             INFO daemonizing the supervisord process

             INFO supervisord started with pid 4510

             INFO spawned: 'apache' with pid 4512

             INFO success: apache entered RUNNING state, process has stayed up for > than 3 seconds (startsecs)

  


    8.测试服务

        wKioL1cN9Y-wYshUAAAeux1DFhc223.png



    9.supervisor web测试是否显现正常

        wKiom1cN9Pryb6wTAACY_aruMrc574.png

 



    10.关闭掉httpd 看是否可以自启动起来

        /usr/local/apache/bin/apachectl stop手动关闭掉httpd

        看日志停止后又启动起来:

              exited: apache (exit status 0; expected)

             INFO spawned: 'apache' with pid 4640

             INFO success: apache entered RUNNING state, process has stayed up for > than 3 seconds (startsecs)








    

另外一种配置文件方法(这样的方法适合多个服务好管理):


1. 修改配置文件 vim /etc/supervisord.conf

[include]

;files = relative/directory/*.ini


修改为:

[include]

;files = relative/directory/*.ini 

files = /etc/supervisord.conf.d/*.conf




2. 创建被管理服务配置文件

      Mkdir  /etc/supervisord.conf.d/

 Cd /etc/supervisord.conf.d/

      Vim apache.conf

  [program:apache]

command=/usr/local/apache/bin/httpd -D FOREGROUND

autostart=true

autorestart=true

startsecs=3

user=root



3. 常用命令

supervisorctl  start  apache

supervisorctl  stop  apache

supervisorctl  status  apache