写在最前:
在项目中总会存在一些意想不到的的问题,导致任务出错中断停止,我们需要去监控任务的执行状态,并当进程执行失败的时候进行重启。
Daemontools 是一个包含了很多管理 Unix 服务的工具的软件包。其中最核心的工具是 supervise,它的功能是监控一个指定的服务,当该服务进程消亡,则重新启动该进程。
而要添加让 supervise 监控的服务非常容易,只需要添加一个被监控的服务的目录,在该目录中添加启动服务器的名字为 run 的脚本文件即可。
当 supervise 被 kill,管理的项目全部死掉,重启 supervise,管理的项目都重启。
1、Supervise 安装
1)获取supervise 安装包:wget http://cr.yp.to/daemontools/daemontools-0.76.tar.gz
2)新建文件夹 supervise
# cd /home/work/
# mkdir supervise
3)解压 daemontools-0.76.tar.gz 到 /home/work/supervise
# tar -zxvf daemontools-0.76.tar.gz -C /home/work/supervise
# cd /home/work/supervise/admin/daemontools-0.76
4)编辑conf-cc,在conf-cc 最后加上:-include /usr/include/errno.h
# vim src/conf-cc
gcc -O2 -Wimplicit -Wunused -Wcomment -Wchar-subscripts -Wuninitialized -Wshadow -Wcast-qual -Wcast-align -Wwrite-strings -include /usr/include/errno.h
This will be used to compile .c files.
5)执行安装命令(此命令需要安装过gcc。如果没有安装gcc 需要安装 gcc )
# cd /home/work/supervise/admin/daemontools-0.76
# ./package/install
// CentOS 安装gcc 命令:yum -y install gcc gcc-c++ autoconf make
# cat /etc/inittab
检查安装(如果出现如下所示则安装成功)
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#
id:3:initdefault:
SV:123456:respawn:/command/svscanboot
2、supervise 使用
supervise 添加监控的服务非常容易,只需要添加一个被监控的服务的目录,在该目录中添加启动服务器的名字为 run 的脚本文件即可。
使用案例:GoBeansDB 全解析