Unix上进程监控脚本

上一篇 / 下一篇  2009-02-09 22:50:03 / 个人分类:Shell 编程

  • 文件版本: V1.0
  • 开发商: 本站原创
  • 文件来源: 本地
  • 界面语言: 简体中文
  • 授权方式: 免费
  • 运行平台: Win9X/Win2000/WinXP
在unix上开发时, 单元测试时常常需要kill掉上次启动的程序, 然后手工启动,  这些程序都是后台进程, 设置参数多, 每次手工做ps--kill--启动命令, 特别麻烦, 所以自己就动手写了个简单的脚本, 提高自己的工作效率, 还可以监控, 自动重起崩溃的后台进程.  源代码如下dog.sh:

0M|o md x051Testing软件测试网*y-a!M\0X/V^2~
#!/bin/bash51Testing软件测试网2q2FN,p!Mug Vq:[
#
KDLo1S3M6G0##########################################################################################
6q[f v\o4QI K0#51Testing软件测试网 ?aYKm^&^J
# name:  dog.sh
Q*D#z_(WX[0# brief: monitor the running program.
!F5CC2Sxi!A0# author:  Robbinson
T7}LY:O&aX8s_*z R0# date:  2007-09-2451Testing软件测试网9b*gENm)t/t&V1m
# args:51Testing软件测试网#c j1QI q#C9`
#
]?9^`K l0##########################################################################################

VZ0GN6}4G051Testing软件测试网RrM?3{%U @w&q


9Xf~2[$V(@0E0##########################################################################################51Testing软件测试网,ueB+z]7WS
#51Testing软件测试网sbiL?`K/W
# brief: If the program is running51Testing软件测试网 Yt l {_9E/CGN
# pram:  $1: a command line from the dog.cnf
%`?K'b,k Y;X6Z0# retval: 0--exit, 1--running, 2--killed
YEz%Q(@0#51Testing软件测试网 h T)u H}Q
##########################################################################################51Testing软件测试网;X'v:vQ};b
is_running( )51Testing软件测试网z;KB `6@vv4}qFR/m{
{51Testing软件测试网l A6D K;f3L:F
 exe_file=`echo $line | awk '{print $1}'`51Testing软件测试网 ?*S*B"E Ws%v^Y
 
7OTe"e4EE8v6Tr0 pid=`ps -e -o pid,args | grep $exe_file | grep -v grep | awk '{print $1}'`
$B tj @!Sr0JT F+h0 if [ "$pid" != "" ]
2ACl L acm;Q0 then
#M.j$a OC)e'p\5M0  if [ "$is_kill" -eq "1" ]51Testing软件测试网O&[.p.wg
  then51Testing软件测试网;^1i(g h-NW
   kill -9 $pid51Testing软件测试网/d{&_xC(C1\1`
   return 251Testing软件测试网;rM7ld[6\X
  else51Testing软件测试网 N$x.a5F2H g
   return 1
qRLR0^Q'e0  fi
4D-Hra5I/T+D*j0 fi

f,s{5rG'zm0

{$c@0W4no.n#f:E0 return 0
S^[9O!h0}

O[!y;]V8V051Testing软件测试网IA5O I5a%y


ky[ Z_0MSO0##########################################################################################51Testing软件测试网'd,AeYSd
#
:Qrf)iw^G0# brief: starup the program
LjH8dO @0# pram:  $1: a command line from the dog.cnf51Testing软件测试网(U Jw"h#z-MY5]
# retval: 0--OK, 1--Fail51Testing软件测试网T-[&AM7GW!T
#51Testing软件测试网?z2o,j6r2~A p
##########################################################################################51Testing软件测试网_a-R#@2CY^4iq
start_up( )
k1J p k@o&JI4M0{
0yKP3k#e&O*\}[0 exe_file=`echo $line | awk '{print $1}'`
eCI'F8Ja0 count=`echo $exe_file | awk -F/ '{print NF}'`
OU8g9AU!@0 count=`expr $count - 1`51Testing软件测试网;O o2p8Mv$lG
 path=`echo $exe_file | cut -d/ -f1-$count`51Testing软件测试网,@]5`7_S b$L

51Testing软件测试网Ic&^sY!A

 cd $path51Testing软件测试网`^"v1fL K
 $line51Testing软件测试网"R1?:P'|Ar
}51Testing软件测试网W/CExQFx ?6F8\+x4A

51Testing软件测试网@~(} W~8^


VL}@.|7B0##########################################################################################51Testing软件测试网.\!PGb'xTD@
#
ZnFM'm$D"]j0# brief: Guard all process from the dog.cnf51Testing软件测试网H.ZN&_#f+u&T6\
#
U"D&^ cxe0u-Y'W0##########################################################################################51Testing软件测试网hPt(]U5}#\s
guard_process( )
'yDy7[6o5I s0{
#~6VTLkO(X0 is_kill=051Testing软件测试网sw%q3Va(Z k

RU.E|V-I0 while read line51Testing软件测试网(D*c4pK6}$cR^n M
 do51Testing软件测试网[%Y a'jUHu [iPo
  case $line in51Testing软件测试网{9n P+J*s/iTN
  \#*)51Testing软件测试网9@5l'J9i.YhO$jXU
   ;; # ignore any hash signs51Testing软件测试网 [i esg0w*Y)A
  *)
PLm9c {b.}p4k0   is_running $line, $is_kill
R2_M] G4_yIH0   if [ $? = 0 ]51Testing软件测试网f_W:Sd2T%A Vl
   then51Testing软件测试网b,m{pD:a Z
    start_up $line
uM(sA+P/i0   fi51Testing软件测试网ye P9_W/gF/HR
   ;;51Testing软件测试网^)U:@K%V
  esac
U*yS3y??6VD:G/n0 done < $dog_cnf
9oCc_p"YZ7`#i2gJ0}

*@JnY7P I ^0

*c6qfi1EKY~051Testing软件测试网3gl J;SZ;lr
##########################################################################################51Testing软件测试网)D;YaMhi0X ~
#51Testing软件测试网-yzgp.DFi+f
# brief: List all process from the dog.cnf51Testing软件测试网C MYEf@hs
#
wQ8D Q(_7bg0##########################################################################################
Lv2b-Q2I"R0list_process( )51Testing软件测试网1GJ^(Q*R ?6C
{51Testing软件测试网'pa&To:`Y)t'MPC l5K
 index=051Testing软件测试网$lr n\&L1n8] Q&K
 is_kill=0

MV{@oK&O%}0

8q!^ OU \hb0 echo "index   status   line"51Testing软件测试网i h*Cl/agl$g
 echo "-------------------------------------------------------------------------------"51Testing软件测试网aKL7E&X8B fc

-z t;OR(s G0N0 while read line51Testing软件测试网K)be8Lki
 do51Testing软件测试网,t dhE/y6[?
  case $line in
-I$z`H6r Oh \#E[T0  \#*)51Testing软件测试网7y_C(by
   ;; # ignore any hash signs51Testing软件测试网o6pt1pO-cp
  *)
BoRT1z0   st_des="run"51Testing软件测试网D)eRr9|
   is_running $line, $is_kill
8XV6lk c4x@c0   if [ $? = 0 ]51Testing软件测试网9N_}I]WH H6W{b*a
   then
Sq*o~{0    st_des="exit"
"~U/@6ax0   fi

S~e7`K%B051Testing软件测试网B Qty1\

   index=`expr $index + 1`51Testing软件测试网 m-aY9y)?6[
   echo "$index       $st_des     $line"
,cb_g"sG0   ;;
w.jRe2W5B0  esac51Testing软件测试网(UTjO(V3i]"T/X
 done < $dog_cnf
8^ x7P|"H-f mjt0}51Testing软件测试网1QQ9o7sp+@_Q

bC&r7vy0
k+et"?"T:tjt0##########################################################################################51Testing软件测试网1@OG*]_.\C
#51Testing软件测试网"]`R8h Z~:l!sH
# brief: List the operate command51Testing软件测试网U'Q6N4~v*Ck;uc
#
mD)iaR6a`*G)J.{0##########################################################################################
H/LX d*rPQ"e0list_command( )51Testing软件测试网!vP \Lyf E
{
,JM:|+r$f!Rc(u*J U}0 echo ""51Testing软件测试网4Hc9um*g?0z$o
 echo "-------------------------------------------------------------------------------"
w$E0OOC*Jw0 echo "Command:"
}-aFL6KLf*zV1R0 echo "  index---restart the specified process by index, index is 1, 2, 3, ..."51Testing软件测试网R@C+I/i)_J},Cdd
 echo "  l----list the all process"51Testing软件测试网i_f^X&uSy
 echo "  e----edit the config file by vi"
#j My5O c'EOl0 echo "  q----quit the dog"
rFVK.c_U0 echo "Please input:"
8z3QjDwcs!B;@*Ja wQ0}

s @;C[o{!o C0

nC [:Ux7z8gSeV051Testing软件测试网%sF+I&kmIv
##########################################################################################
L!Ssh%\*~0#
H!fj)N4^E0# brief: restart the specified process51Testing软件测试网 z A(R7Q5L1]}-q+p
#
p2XTa n&I VW0##########################################################################################51Testing软件测试网 W,is1t5hc2QI(qT
restart_process( )51Testing软件测试网u H td@2y
{51Testing软件测试网dtj:k%\
 restart_index=0
oV%}3U DZ6TW0 is_kill=151Testing软件测试网x s|9TgVVv
 is_restart=0

H&H{Ay051Testing软件测试网_+N }/Fz:S+LVU6d

 while read line
7\/G6K9`#?0 do51Testing软件测试网N)\hwOg ~7su-d
  case $line in51Testing软件测试网#l/}@w+Np
  \#*)51Testing软件测试网!m {lF5vH3P.BK1p#a m'}
   ;; # ignore any hash signs51Testing软件测试网'h} s3j8~\
  *)51Testing软件测试网VP#`5Pf3\,I
   restart_index=`expr $restart_index + 1`

%q{.CCeI? k0

S6Y.Z I0TEO{}0   if [ "$command" = "$restart_index" ]
T GwEj7`p E ij\0   then
9P5{.Y)J[:d0    is_running $line, $is_kill51Testing软件测试网*d8W1a-F/~` ?'z
    if [ $? != 1 ]
4`6pmF-s7e$R0    then
%DO0m l&gZ0     start_up $line51Testing软件测试网+_/Q]4rQv xJ:O
     is_restart=151Testing软件测试网$IhJ+pa
     break51Testing软件测试网QHI @ D
    fi51Testing软件测试网7D%j"A]d\n
   fi
5dF?Z N\h#zH4f0   ;;
:J2n8n S { k0  esac51Testing软件测试网5\2}g+k]!gd ^8i
 done < $dog_cnf51Testing软件测试网/m+ijh0|5[ w%`7PN

51Testing软件测试网6Y:P.rS$~;q&?R`

 if [ "$is_restart" = "1" ]
a9\{x'z0Y&u0 then
Ltt L9W0  echo "restart OK!"51Testing软件测试网N1iB.LT8K:b
 else
&nS:p0Lp$MM[;U C3k;K0  echo "$command is not invalid index!"
/M;X q` C0 fi51Testing软件测试网5tT%X}/_\;r4B
}

0@2{]7]3EM$s b r'D051Testing软件测试网E{\i1~

51Testing软件测试网Q*eR7Q5s wJ
##########################################################################################51Testing软件测试网oR-EyX#b#\x
#
q0\-jiNU0# brief: Manage the process
#L_6F2BD/^ ^(a(}r0#
\_L3k7q$x,E0##########################################################################################
!}{y` si0manage_process( )51Testing软件测试网1W~K1u.G C
{51Testing软件测试网'n&}KCY:R0@&[
 index=0
J1] ~ X,c,y0 list_process $dog_cnf, $index51Testing软件测试网`a9Y;}"N W6F;G
 list_command

N e(BVZ0

I Oy0DpR0 while read command
pN~u0?}#E0 do51Testing软件测试网Uw w;O9v4Vb
  case $command in
|S"Zn)o_{0  q)51Testing软件测试网 y1H eXdf0?f
   echo "bye!!!"
6B)Y}r p `N3O9k0   break;
:qlP'H k9HAA0   ;;
.@|HRy#u.]0  e)51Testing软件测试网+FR)By^y7]6E
   vi $dog_cnf
2e2lO!G0F0   list_process $dog_cnf, $index51Testing软件测试网]#K9c"b$]$J2Q
   list_command51Testing软件测试网x/\ vz/c J9@5v1V3f+@
   ;;
[Nu*n XS| H(^ O0  l)
Y SOR_ i#e0   list_process $dog_cnf, $index51Testing软件测试网"q1^|Ec*e
   list_command51Testing软件测试网O/xz4Z5IUsL({
   ;;
)h"v;|^x2z^+^(|:b0  *)51Testing软件测试网Ca?-WG.B0i8B @
   restart_process $dog_cnf, $command51Testing软件测试网|%`1{\,Rrw
   list_command51Testing软件测试网w&n.Y4X!D8c$f}
   ;;
aTyBw0  esac
g j(f^`0 done
3J(^ [C5?%w%KAH0}

i.o!{(|@({H0

\#piKl s5_x])N051Testing软件测试网+Vk'~.Vx!L I8rk`D
##########################################################################################
:@)J}6V?um0#51Testing软件测试网 x}@ZIN }C G
# brief: Print the help51Testing软件测试网5`*AH5g4[su
#
e;i;j0G Y`4b0##########################################################################################
;tIdF td i0w p0usage( )
)IE9Eu+h&|0{
'\PZb3e,k s\6P0 echo "------------------dog.sh-----------------------"
nOy1Y3T/e0 echo "  -h----print the help"51Testing软件测试网"Xe}N.rz
 echo "  -v----print the help"
pn!wXSN(J/Y0 echo "  -e----edit the config file by vi"51Testing软件测试网6i;\Hs!xA3D
 echo "  -d----guard all processes in background"51Testing软件测试网~ A2A/PS5ld[
 echo "  -f----set the config file"51Testing软件测试网BIr YYK
 echo "  -s----the second to sleep"
[Bc y-O v*}0 echo "  examples:"51Testing软件测试网!uT0N#@7r m?N
 echo "    1. guard all processes:"51Testing软件测试网ga DG#D:Zlx
 echo "       nohup dog.sh -d &"
E%O l3D)g"[0 echo "       nohup dog.sh -d -s 60 &"
g O \-Q'}h3l0 echo "    2. list all processes:"51Testing软件测试网 Z.XHz }tu(d
 echo "       dog.sh"
l6~{-EPgV"j{y2Q0}

(L` fr(z2U0

,p&m1z'H!y0
^d\#s,a K0##########################################################################################51Testing软件测试网_Rx7A2g#bt0Z1^ O z
#51Testing软件测试网bV3e| a JQi5\F
# brief: The main module51Testing软件测试网]a Mw @!X
#51Testing软件测试网'j*mbfe)o4s(Es(H
##########################################################################################
y(C9YXq/oZ y0is_guard=051Testing软件测试网3fS ~-s"G0{7c S Jx5zA5Q
sleep_sec=3051Testing软件测试网k9kmiC-q3tu
dog_cnf=./dog.cnf

g }"y"e1I~#E0

,M J:G{0h`0while getopts hveds:f: option51Testing软件测试网 rzH NOR)l.H!R
do
,d2i7Ii},j8b&Ls'Ts0 case $option in
W/s'Q!L\7|0 h)51Testing软件测试网}6^ pNYF|}P
  usage
2B%a3H+ymqb2_0  exit 0 ;;51Testing软件测试网BBTIxn$|m(N
 v)51Testing软件测试网Tu?lAs`*}2f
  usage51Testing软件测试网uOMgOu u
  exit 0 ;;51Testing软件测试网3}*j;^1DJ
 e)51Testing软件测试网"~und"YS
  vi $dog_cnf
gC`E A7NRi0  ;;51Testing软件测试网;zm4Z;B,v/wz B)c'h
 d)
eBh+Ddiw6q [0  is_guard=1 ;;
1d)Ak&{|]"p%H0 s)51Testing软件测试网CKaq6C+Z4M
  sleep_sec=$OPTARG ;;51Testing软件测试网*A9r5yA5V$d
 f)51Testing软件测试网hp:? [:~.? ^d$eC F
  dog_cnf=$OPTARG ;;
ttu4\"XU0 esac
}"c_(Ca0done51Testing软件测试网:Lt3B:z+]X~mv

k:D,ptU0if [ "$is_guard" = "0" ]51Testing软件测试网4`oNeXsQ9tN1h]
then
`b5Wt&eW0 manage_process $dog_cnf51Testing软件测试网jhMV0blZ9r
else51Testing软件测试网3R0ES!B+q
 while [ "1" = "1" ]51Testing软件测试网,r uA%SlB
 do
5M2O4l7DF'|'k o0  guard_process $dog_cnf
8iT"d)c Q/D f |af0  sleep $sleep_sec51Testing软件测试网7x6vL{7v:viS&h
 done51Testing软件测试网s^T{ X j
fi51Testing软件测试网s q"h$aU;~5N

m+qg3RW6Qk%B\"H051Testing软件测试网 y4EJ;\e;kKc
# the end of the shell
#g K\'Y$^051Testing软件测试网u1M0w9i#``P

].W#Q)Rh-c0配置文件dog.cnf如下:  注意一般这些都是后台进程, 一次性运行的没必要这么做51Testing软件测试网${\,r,jO'P s(SJ
# command
"~z/xT)~[7g0/home/test/smtpserver -d
3O1G#fdbP \'?%\`0/home/test/pop3server -d
.u1`@:|5`!N/h{0/home/test/imailserver -d51Testing软件测试网{o$L/vj$yIr3z
/home/test/apache/bin/httpd start51Testing软件测试网$fBo^*h0Y

9o#]nF:H^E} p8S0

TAG:

 

评分:0

我来说两句

Open Toolbar