supervisor

supervisor的安装和详细使用说明

作用:有一天我们的开发和我说,服务器上面的一个php的分粉的进程经常的掉,老是需要手动起来,我呢又不想写啥脚本,判断脚本,因为我菜的很,写出来也不好用,也想着兼容一些其他的东西,后来,我就搞了个这个,又向自动化运维进步了一步,哈哈哈

一.安装方式

yum安装

$ yum install -y supervisor
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile

  • base: mirrors.aliyun.com
  • extras: mirrors.aliyun.com
  • updates: mirrors.aliyun.com
    没有可用软件包 supervisor。
    错误:无须任何处理



只好作罢!!!

### tar包安装

```也可以在次连接下载:[](https://download.csdn.net/download/weixin_56319285/86791601)
# root @ localhost in ~/tools [10:06:00]

$ wget https://pypi.python.org/packages/80/37/964c0d53cbd328796b1aeb7abea4c0f7b0e8c7197ea9b0b9967b7d004def/supervisor-3.3.1.tar.gz
$ tar zxf supervisor-3.3.1.tar.gz
$ cd supervisor-3.3.1 

# root @ localhost in ~/tools/supervisor-3.3.1 [10:06:35]

$ python setup.py install  #我这里使用的是python2.7.5
Installed /usr/lib/python2.7/site-packages/supervisor-3.3.1-py2.7.egg
Processing dependencies for supervisor==3.3.1
Searching for meld3>=0.6.5
Reading https://pypi.python.org/simple/meld3/
Couldn't find index page for 'meld3' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
No local packages or download links found for meld3>=0.6.5
error: Could not find suitable distribution for Requirement.parse('meld3>=0.6.5')

# root @ localhost in ~/tools [10:08:01]

$ wget https://pypi.python.org/packages/45/a0/317c6422b26c12fe0161e936fc35f36552069ba8e6f7ecbd99bbffe32a5f/meld3-1.0.2.tar.gz\#md5\=3ccc78cd79cffd63a751ad7684c02c91
$ tar zxvf meld3-1.0.2.tar.gz
$ cd meld3-1.0.2

# root @ localhost in ~/tools/meld3-1.0.2 [10:08:18]

$ python setup.py install

# root @ localhost in ~/tools/supervisor-3.3.1 [10:08:42]

$ python setup.py install

二.配置

1.生成配置文件

# root @ localhost in ~/tools/supervisor-3.3.1 [10:21:41]

$ echo_supervisord_conf > /etc/supervisord.conf

2.启动

# root @ localhost in ~/tools/supervisor-3.3.1 [10:22:15] 

$ supervisord -c /etc/supervisord.conf

3.查看

# root @ localhost in ~/tools/supervisor-3.3.1 [10:22:29] 

$ ps aux | grep supervisord                    
root      24781  0.0  0.3 215576 10000 ?        Ss   10:22   0:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root      24788  0.0  0.0 112824   992 pts/0    S+   10:22   0:00 grep --color=auto supervisord

4.基础配置

(1)打开配置文件
# root @ localhost in ~ [10:35:12] 

$ vim /etc/supervisord.conf 
(2)在配置文件底部,配置include
[include]
;files = relative/directory/*.ini
files=/etc/supervisor/*.conf  #若你本地无/etc/supervisor目录,请自建
(3)用supervisor管理进程,配置如下
# root @ localhost in ~ [10:38:29]
$ mkdir /etc/supervisor
$ cd /etc/supervisor
$ vim ossfs.conf # 这里的文件名称自定义
(4)加入以下内容
; 设置进程的名称,使用 supervisorctl 来管理进程时需要使用该进程名
[program:your_program_name] 
command=python server.py --port=9000
;numprocs=1                 ; 默认为1
;process_name=%(program_name)s   ; 默认为 %(program_name)s,即 [program:x] 中的 x
directory=/home/python/tornado_server ; 执行 command 之前,先切换到工作目录
user=oxygen                 ; 使用 oxygen 用户来启动该进程
; 程序崩溃时自动重启,重启次数是有限制的,默认为3次
autorestart=true            
redirect_stderr=true        ; 重定向输出的日志
stdout_logfile = /var/log/supervisord/tornado_server.log     #你执行的命令输出的日志,当然也包含,你的这个进程,使用supervisor执行时可能报错的一些日志
loglevel=info
(5)服务启动
# root @ localhost in /etc/supervisor [10:41:54] 
$ supervisord -c /etc/supervisord.conf 
Error: Invalid user name oxygen in section 'program:your_program_name' (file: '/etc/supervisor/ossfs.conf')
For help, use /usr/bin/supervisord -h

这里报错的原因是我刚才的ossfs.conf文件里面的user没有创建,大家使用的时候,可以更具自己的项目去配置,我这里只是做个演示

加载配置才是第一步,其他的都是浮云,只有做完了这一步,下才能保证服务的完整性,不然服务都找不到配置文件,就玩完了。

(6)这里是启动要配置的参数,请根据自己的项目自定义添加,更改了supervisor配置文件,需要重启,运行以下指令
# root @ localhost in /etc/supervisor [10:41:42]
$ supervisorctl reload
Restarted supervisord
(7)用浏览器来管理进程配置(一般很少用,特别是在服务器上面,暴露公网是不安全的)
1.打开配置文件
# root @ localhost in ~ [10:45:29] 

$ vim /etc/supervisord.conf 

[inet_http_server]         ; inet (TCP) server disabled by default
port=0.0.0.0:9001          ; (ip_address:port specifier, *:port for all iface)
username=root              ; (default is no username (open server))
password=123.com           ; (default is no password (open server))

三、supervisorctl的用法

supervisord : 启动supervisor

supervisorctl reload :修改完配置文件后重新启动supervisor

supervisorctl status :查看supervisor监管的进程状态

supervisorctl start all | 进程名 :启动全部或某进程

supervisorctl stop all | 进程名 :停止全部或某进程

supervisorctl stop all:停止进程,注:start、restart、stop都不会载入最新的配置文件。

supervisorctl update:根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启

四、设置开机启动

(1)添加开机脚本

# root @ localhost in ~ [10:52:12]

$ vim /etc/init.d/supervisord

#! /bin/sh
### BEGIN INIT INFO
# Provides:          supervisord
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

# Author: Dan MacKinlay <danielm@phm.gov.au>
# Based on instructions by Bertrand Mathieu
# http://zebert.blogspot.com/2009/05/installing-django-solr-varnish-and.html

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
DESC="Description of the service"
NAME=supervisord
DAEMON=/usr/local/bin/supervisord
DAEMON_ARGS=" -c /etc/supervisord.conf"
#PIDFILE=/var/run/$NAME.pid
PIDFILE=/tmp/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
        || return 1
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
        $DAEMON_ARGS \
        || return 2
    # Add code here, if necessary, that waits for the process to be ready
    # to handle requests from services started subsequently which depend
    # on this one.  As a last resort, sleep for some time.
}

#
# Function that stops the daemon/service
#
do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    # Wait for children to finish too if this is a daemon that forks
    # and if the daemon is only ever run from this initscript.
    # If the above conditions are not satisfied then add some other code
    # that waits for the process to drop all resources that could be
    # needed by services started subsequently.  A last resort is to
    # sleep for some time.
    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
    [ "$?" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.
    rm -f $PIDFILE
    return "$RETVAL"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
    #
    # If the daemon can reload its configuration without
    # restarting (for example, when it is sent a SIGHUP),
    # then implement that here.
    #
    start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
    return 0
}

case "$1" in
  start)
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
    do_start
    case "$?" in
        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
  stop)
    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
    do_stop
    case "$?" in
        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
  #reload|force-reload)
    #
    # If do_reload() is not implemented then leave this commented out
    # and leave 'force-reload' as an alias for 'restart'.
    #
    #log_daemon_msg "Reloading $DESC" "$NAME"
    #do_reload
    #log_end_msg $?
    #;;
  restart|force-reload)
    #
    # If the "reload" option is implemented then remove the
    # 'force-reload' alias
    #
    log_daemon_msg "Restarting $DESC" "$NAME"
    do_stop
    case "$?" in
      0|1)
        do_start
        case "$?" in
            0) log_end_msg 0 ;;
            1) log_end_msg 1 ;; # Old process is still running
            *) log_end_msg 1 ;; # Failed to start
        esac
        ;;
      *)
        # Failed to stop
        log_end_msg 1
        ;;
    esac
    ;;
  *)
    #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
    exit 3
    ;;
esac
:

(2)设置该脚本为可以执行

# root @ localhost in ~ [10:52:28] 

$ sudo chmod +x /etc/init.d/supervisord

(3)设置为开机自动运行

# root @ localhost in ~ [10:53:24] 

$ sudo update-rc.d supervisord defaults

这种配置开机自启的方式,有点不好的地方:

1.如果你配置错了,且你没有验证的机会,因为这可能是生产环境

2.当你需要升级ecs的配置,如果以上配置错误,会导致你的服务器都启动不了,因为他是放在开机自检,启动的时候,所以有一定的风险性

(4)试一下,是否工作正常

# root @ localhost in ~ [10:55:47] C:127

$ service supervisord stop
$ service supervisord start

(2)第二种配置开机自启的方法

1.找到相关命令的路径
# root @ localhost in ~/tools [15:43:03]

$ which supervisorctl
/usr/bin/supervisorctl
$ which supervisord  
/usr/bin/supervisord
2.建立supervisord.service文件
# root @ localhost in ~ [15:46:48] 
$ cd /usr/lib/systemd/system/
$ vim supervisord.service
#supervisord.service
 
[Unit]
Description=Supervisor daemon
 
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s
 
[Install]
WantedBy=multi-user.target
3.设置开机自启动
$ systemctl enable supervisord.service
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

客官,吃辣条吗?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值