梁 兄

QQ: 160216918 QQ群: 26678700 MSN: lb_bing@hotmail.com

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  52 Posts :: 5 Stories :: 355 Comments :: 0 Trackbacks

         在unix上开发时, 单元测试时常常需要kill掉上次启动的程序, 然后手工启动,  这些程序都是后台进程, 设置参数多, 每次手工做ps--kill--启动命令, 特别麻烦, 所以自己就动手写了个简单的脚本, 提高自己的工作效率, 还可以监控, 自动重起崩溃的后台进程.  源代码如下dog.sh:


#!/bin/bash
#
##########################################################################################
#
# name:  dog.sh
# brief: monitor the running program.
# author:  Robbinson
# date:  2007-09-24
# args:
#
##########################################################################################


##########################################################################################
#
# brief: If the program is running
# pram:  $1: a command line from the dog.cnf
# retval: 0--exit, 1--running, 2--killed
#
##########################################################################################
is_running( )
{
 exe_file=`echo $line | awk '{print $1}'`
 
 pid=`ps -e -o pid,args | grep $exe_file | grep -v grep | awk '{print $1}'`
 if [ "$pid" != "" ]
 then
  if [ "$is_kill" -eq "1" ]
  then
   kill -9 $pid
   return 2
  else
   return 1
  fi
 fi

 return 0
}


##########################################################################################
#
# brief: starup the program
# pram:  $1: a command line from the dog.cnf
# retval: 0--OK, 1--Fail
#
##########################################################################################
start_up( )
{
 exe_file=`echo $line | awk '{print $1}'`
 count=`echo $exe_file | awk -F/ '{print NF}'`
 count=`expr $count - 1`
 path=`echo $exe_file | cut -d/ -f1-$count`

 cd $path
 $line
}


##########################################################################################
#
# brief: Guard all process from the dog.cnf
#
##########################################################################################
guard_process( )
{
 is_kill=0

 while read line
 do
  case $line in
  \#*)
   ;; # ignore any hash signs
  *)
   is_running $line, $is_kill
   if [ $? = 0 ]
   then
    start_up $line
   fi
   ;;
  esac
 done < $dog_cnf
}


##########################################################################################
#
# brief: List all process from the dog.cnf
#
##########################################################################################
list_process( )
{
 index=0
 is_kill=0

 echo "index   status   line"
 echo "-------------------------------------------------------------------------------"

 while read line
 do
  case $line in
  \#*)
   ;; # ignore any hash signs
  *)
   st_des="run"
   is_running $line, $is_kill
   if [ $? = 0 ]
   then
    st_des="exit"
   fi

   index=`expr $index + 1`
   echo "$index       $st_des     $line"
   ;;
  esac
 done < $dog_cnf
}


##########################################################################################
#
# brief: List the operate command
#
##########################################################################################
list_command( )
{
 echo ""
 echo "-------------------------------------------------------------------------------"
 echo "Command:"
 echo "  index---restart the specified process by index, index is 1, 2, 3, ..."
 echo "  l----list the all process"
 echo "  e----edit the config file by vi"
 echo "  q----quit the dog"
 echo "Please input:"
}


##########################################################################################
#
# brief: restart the specified process
#
##########################################################################################
restart_process( )
{
 restart_index=0
 is_kill=1
 is_restart=0

 while read line
 do
  case $line in
  \#*)
   ;; # ignore any hash signs
  *)
   restart_index=`expr $restart_index + 1`

   if [ "$command" = "$restart_index" ]
   then
    is_running $line, $is_kill
    if [ $? != 1 ]
    then
     start_up $line
     is_restart=1
     break
    fi
   fi
   ;;
  esac
 done < $dog_cnf

 if [ "$is_restart" = "1" ]
 then
  echo "restart OK!"
 else
  echo "$command is not invalid index!"
 fi
}


##########################################################################################
#
# brief: Manage the process
#
##########################################################################################
manage_process( )
{
 index=0
 list_process $dog_cnf, $index
 list_command

 while read command
 do
  case $command in
  q)
   echo "bye!!!"
   break;
   ;;
  e)
   vi $dog_cnf
   list_process $dog_cnf, $index
   list_command
   ;;
  l)
   list_process $dog_cnf, $index
   list_command
   ;;
  *)
   restart_process $dog_cnf, $command
   list_command
   ;;
  esac
 done
}


##########################################################################################
#
# brief: Print the help
#
##########################################################################################
usage( )
{
 echo "------------------dog.sh-----------------------"
 echo "  -h----print the help"
 echo "  -v----print the help"
 echo "  -e----edit the config file by vi"
 echo "  -d----guard all processes in background"
 echo "  -f----set the config file"
 echo "  -s----the second to sleep"
 echo "  examples:"
 echo "    1. guard all processes:"
 echo "       nohup dog.sh -d &"
 echo "       nohup dog.sh -d -s 60 &"
 echo "    2. list all processes:"
 echo "       dog.sh"
}


##########################################################################################
#
# brief: The main module
#
##########################################################################################
is_guard=0
sleep_sec=30
dog_cnf=./dog.cnf

while getopts hveds:f: option
do
 case $option in
 h)
  usage
  exit 0 ;;
 v)
  usage
  exit 0 ;;
 e)
  vi $dog_cnf
  ;;
 d)
  is_guard=1 ;;
 s)
  sleep_sec=$OPTARG ;;
 f)
  dog_cnf=$OPTARG ;;
 esac
done

if [ "$is_guard" = "0" ]
then
 manage_process $dog_cnf
else
 while [ "1" = "1" ]
 do
  guard_process $dog_cnf
  sleep $sleep_sec
 done
fi


# the end of the shell


配置文件dog.cnf如下:  注意一般这些都是后台进程, 一次性运行的没必要这么做
# command
/home/test/smtpserver -d
/home/test/pop3server -d
/home/test/imailserver -d
/home/test/apache/bin/httpd start

posted on 2007-11-23 09:30 梁-兄 阅读(954) 评论(2)  编辑 收藏 引用 所属分类: 脚本语言

Feedback

# re: Unix上进程监控脚本 2007-11-23 10:44 泡泡牛
killall 不就得了  回复  更多评论
  

# re: Unix上进程监控脚本 2007-11-23 11:30 Jedi
你有试过pkill命令吗?linux下面有这个命令。可以直接根据进程名称来kill。很方便的  回复  更多评论
  


标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]