|
| 1 | +# daemontools 工具 |
| 2 | + |
| 3 | +## supervisord |
| 4 | + |
| 5 | +- 注意:Supervisor 能管理非 daemon 的进程,也就是说 Supervisor 不能管理守护进程。否则提示 Exited too quickly (process log may have details) 异常。 |
| 6 | +- 官网:<http://supervisord.org/installing.html> |
| 7 | +- 安装过程: |
| 8 | + - 解释:easy_install 是 setuptools 包里带的一个命令,使用 easy_install 实际上是在调用 setuptools 来完成安装模块的工作,所以安装 setuptools 即可。 |
| 9 | + - 安装: |
| 10 | + - `yum -y install python-setuptools` |
| 11 | + - `easy_install supervisor` |
| 12 | + - 生成配置文件: |
| 13 | + - `echo_supervisord_conf > /etc/supervisord.conf` |
| 14 | + - 创建专门的程序配置文件目录、日志目录: |
| 15 | + - `mkdir -p /var/log/supervisor` |
| 16 | + - `mkdir -p /etc/supervisor/conf.d/` |
| 17 | + - `echo -e "[include]\nfiles = /etc/supervisor/conf.d/*.conf">>/etc/supervisord.conf` |
| 18 | +- 安装完成的内容介绍:supervisor 安装完成后会生成三个执行程序: |
| 19 | + - supervisortd:supervisor 的守护进程服务(用于接收进程管理命令) |
| 20 | + - supervisorctl:客户端(用于和守护进程通信,发送管理进程的指令) |
| 21 | + - echo_supervisord_conf:生成初始配置文件程序。 |
| 22 | +- 程序位置:`/usr/bin/supervisord` |
| 23 | +- 配置文件位置:`/etc/supervisord.conf` |
| 24 | + |
| 25 | +### Logstash 进程进行守护 |
| 26 | + |
| 27 | +- 默认安装完 Supervisor 是已经启动的,所以在加入新配置之前,需要先停止程序:`ps -ef | grep supervisord`,kill 对应的 pid |
| 28 | +- 创建配置文件:`vim /etc/supervisor/conf.d/logstash.conf` |
| 29 | + |
| 30 | +``` nginx |
| 31 | +[program:gitnavi-logstash] |
| 32 | +command=/usr/program/elk/logstash-2.4.1/bin/logstash -f /usr/program/elk/logstash-2.4.1/config/logstash.conf |
| 33 | +stdout_logfile=/var/log/supervisor/supervisord-logstash.log |
| 34 | +stderr_logfile=/var/log/supervisor/supervisord-logstash-err.log |
| 35 | +user=root |
| 36 | +autostart=true |
| 37 | +autorestart=true |
| 38 | +startsecs=5 |
| 39 | +priority=1 |
| 40 | +stopasgroup=true |
| 41 | +killasgroup=true |
| 42 | +``` |
| 43 | + |
| 44 | +- 启动程序(默认会启动所有子任务):`/usr/bin/supervisord -c /etc/supervisord.conf` |
| 45 | +- 管理子任务的命令: |
| 46 | + - 启动所有子任务:`/usr/bin/supervisorctl start all` |
| 47 | + - 结束所有子任务:`/usr/bin/supervisorctl stop all` |
| 48 | + - 只载入最新的配置文件, 并不重启任何进程:`/usr/bin/supervisorctl reread` |
| 49 | + - 载入最新的配置文件,停止原来的所有进程并按新的配置启动管理所有进程:`/usr/bin/supervisorctl reload` |
| 50 | + - 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启:`/usr/bin/supervisorctl update` |
| 51 | + - 查看所有子任务状态,如果没有运行的子任务则是没有任何反馈信息:`/usr/bin/supervisorctl status` |
| 52 | +- 管理所有子任务也可以用交互方式,输入命令:`supervisorctl`,会进入 supervisord 的交互模式下,如果当前有启动的任务,还可以看到对应的任务情况。 |
| 53 | + - 在该交互下可以停止指定名称的子任务,比如 logstash 任务:`stop gitnavi-logstash` |
| 54 | + - 也可以停止所有子任务:`stop all` |
| 55 | + - 也可以启动所有子任务:`start all` |
| 56 | + - 更多命令可以输入:`help` |
| 57 | + |
| 58 | +### 设置 supervisord 开启自启动 |
| 59 | + |
| 60 | +#### CentOS 6 |
| 61 | + |
| 62 | +- 创建文件:`vim /etc/init.d/supervisord` |
| 63 | + |
| 64 | +``` nginx |
| 65 | +#!/bin/sh |
| 66 | +# |
| 67 | +# Supervisor is a client/server system that |
| 68 | +# allows its users to monitor and control a |
| 69 | +# number of processes on UNIX-like operating |
| 70 | +# systems. |
| 71 | +# |
| 72 | +# chkconfig: - 64 36 |
| 73 | +# description: Supervisor Server |
| 74 | +# processname: supervisord |
| 75 | +# Source init functions |
| 76 | +. /etc/init.d/functions |
| 77 | +RETVAL=0 |
| 78 | +prog="supervisord" |
| 79 | +pidfile="/tmp/supervisord.pid" |
| 80 | +lockfile="/var/lock/subsys/supervisord" |
| 81 | +start() |
| 82 | +{ |
| 83 | +echo -n $"Starting $prog: " |
| 84 | +daemon --pidfile $pidfile supervisord -c /etc/supervisord.conf |
| 85 | +RETVAL=$? |
| 86 | +echo |
| 87 | +[ $RETVAL -eq 0 ] && touch ${lockfile} |
| 88 | +} |
| 89 | +stop() |
| 90 | +{ |
| 91 | +echo -n $"Shutting down $prog: " |
| 92 | +killproc -p ${pidfile} /usr/bin/supervisord |
| 93 | +RETVAL=$? |
| 94 | +echo |
| 95 | +if [ $RETVAL -eq 0 ] ; then |
| 96 | +rm -f ${lockfile} ${pidfile} |
| 97 | +fi |
| 98 | +} |
| 99 | +case "$1" in |
| 100 | +start) |
| 101 | +start ;; |
| 102 | +stop) stop ;; |
| 103 | +status) |
| 104 | +status $prog ;; |
| 105 | +restart) |
| 106 | +stop |
| 107 | +start ;; |
| 108 | +*) |
| 109 | +echo "Usage: $0 {start|stop|restart|status}" ;; |
| 110 | +esac |
| 111 | +``` |
| 112 | + |
| 113 | + |
| 114 | +- `chmod 755 /etc/init.d/supervisord` |
| 115 | +- `chkconfig supervisord on` |
| 116 | +- 以后启动可以用:`service supervisord start` |
| 117 | +- 以后停止可以用:`service supervisord stop` |
| 118 | + |
| 119 | + |
| 120 | +#### CentOS 7 |
| 121 | + |
| 122 | +- 创建文件:`vim /lib/systemd/system/supervisor.service` |
| 123 | + |
| 124 | +``` ini |
| 125 | +[Unit] |
| 126 | +Description=supervisor |
| 127 | +After=network.target |
| 128 | + |
| 129 | +[Service] |
| 130 | +Type=forking |
| 131 | +ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf |
| 132 | +ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown |
| 133 | +ExecReload=/usr/bin/supervisorctl $OPTIONS reload |
| 134 | +KillMode=process |
| 135 | +Restart=on-failure |
| 136 | +RestartSec=42s |
| 137 | + |
| 138 | +[Install] |
| 139 | +WantedBy=multi-user.target |
| 140 | +``` |
| 141 | + |
| 142 | +- `chmod 766 /lib/systemd/system/supervisor.service` |
| 143 | +- `systemctl enable supervisor.service` |
| 144 | +- `systemctl daemon-reload` |
| 145 | + |
| 146 | +## 资料 |
| 147 | + |
| 148 | +- <http://blog.csdn.net/xyang81/article/details/51555473> |
| 149 | +- <https://www.fangc.xyz/detail/centos6pei-zhi-supervisorkai-j/> |
| 150 | +- <http://cpper.info/2016/04/14/supervisor-usage.html> |
| 151 | +- <https://luckymrwang.github.io/2016/12/23/Supervisor%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8/> |
| 152 | +- <http://www.aichengxu.com/linux/24569479.htm> |
| 153 | +- <http://www.tianfeiyu.com/?p=2450> |
0 commit comments