centos6 服务器上为应用进程添加守护

原创
2020/11/20 09:58
阅读数 249

为应用进程添加守护,在应用意外退出或服务器重启时,保证应用会被自动重启

安装和配置守护程序(supervisor)

  1. easy_install supervisor (下载安装supervisor)
  2. /usr/local/bin/echo_supervisord_conf > /etc/supervisord.conf  (添加配置文件,从default复制)
  3. printf "[include]\nfiles = supervisor/conf.d/*.conf\n" >> /etc/supervisord.conf (在配置文件中指定子配置位置)
  4. mkdir  -p   /etc/supervisor/conf.d/  (创建子配置文件目录)
  5. mkdir -p /opt/deploy/  (创建supervisor启动配置目录)
  6. vim /opt/deploy/supervisord (添加supervisor启动配置,内容如下:
    #!/bin/sh
    # Source function library.
    . /etc/rc.d/init.d/functions
    supervisorctl="/usr/local/bin/supervisorctl"
    supervisord="/usr/local/bin/supervisord"
    name="supervisor-python"
    [ -f $supervisord ] || exit 1
    [ -f $supervisorctl ] || exit 1
    RETVAL=0
    start() {
         echo -n "Starting $name: "
         $supervisord -c /etc/supervisord.conf
         RETVAL=$?
         echo
         return $RETVAL
    }
    stop() {
         echo -n "Stopping $name: "
        $supervisorctl shutdown
         RETVAL=$?
         echo
         return $RETVAL
    }
    case "$1" in
             start)
                 start
                 ;;
             stop)
                 stop
                 ;;
             restart)
                 stop
                 start
                 ;;
    esac
    exit $REVAL

  7. chmod +x /opt/deploy/supervisord (添加可执行权限)

  8. ln -s /opt/deploy/supervisord /etc/init.d (注册supervisord,使其可以使用service xxx restart/stop/start)

  9. chkconfig supervisord on (改变服务的启动信息)

 

添加需要守护的应用  

  1. vim  /etc/supervisor/conf.d/xxx.conf (添加xxx应用的守护配置,内容如下:
    [program:xxxx]
    command=/usr/bin/java -Xms2048m -Xmx2048m  -server -jar /home/ec2-user/xxx/xxx.jar
    user=ec2-user
    directory=/home/ec2-user/xxx/logs/..
    autostart=true
    autorestart=true
    startsecs=30
    startretries=1
  2. service supervisord start (启动守护服务)

  3. supervisorctl reload (添加或更新xxx.conf后执行这个命令可以使配置更新)

  4. supervisorctl update (添加或更新xxx.conf后执行这个命令可以使改动立即生效)

其他命令

  1. supervisorctl status (列出被守护应用)

  2. supervisorctl start/stop xxx  (启动/停止xxx应用,notes:用service xxx stop/kill -9 *** 停止某个应用服务会失效,因为会被守护重启,所以如果要主动停止某个应用要使用这行命令 supervisorctl stop xxx

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部