Linux BusyBox 果真是个强大的东西。只要想做,什么都能做出来。正好最近正愁 My Book 的服务总是莫名其妙的挂掉,搞的在单位都无法访问文件。操起简单的 vi,开始制作 Bash Watchdog。(之所以没用 Perl,是考虑到 Perl 在预编译的时候比较耗费系统资源)

经过三个小时的现学现卖,雏形版的 Bash Watchdog - swatchdog 出炉。

/usr/sbin/swatchdog
  1. #! /bin/bash
  2. #
  3. # Watchdog for system service
  4. #  
  5.   
  6. while [ 0 ]  
  7. do
  8.   for PROC
  9.   do
  10.     PID=`ps -aef | grep -v grep | grep -v swatchdog | grep "$PROC"`
  11.     if [ -z "$PID" ] ; then
  12.       TIME=`date "+%Y-%m-%d %H:%M:%S %Z"`
  13.       echo "[$TIME] $PROC is not found, rebooting..." >> /etc/swatchdog/reboot.log
  14.       reboot
  15.       exit 1;
  16.     fi
  17.   done
  18.   sleep 600
  19. done
  20.  
  21. exit 0;

原理很简单,无限循环,检查命令行传入的进程是否存在。如果不存在则重启。

使用也很简单。

Command
  1. swatchdog cvm transmission amuled

自动侦测 cvm(mionet)、transmission 和 amuled。