我们在线上跑的服务,不知道为什么最近有几回运行的进程莫名其妙的就没有了,就特意写了这个监控脚本,让其自动扫描服务,把访问不正常的服务,自动启动或重启服务,并且导出当时的线程使用情况,方便定位问题。

步骤:
1.修改web服务名称和端口
monitorTcp.sh

2.修改扫描时间
monitorServer.sh

3.启动守候进程
/usr/bin/nohup /bin/sh /home/Gzh/shell/monitorServer.sh 2>&1 > /dev/null &

monitorServer.sh

Shell代码 复制代码 收藏代码
  1. #!/bin/sh
  2. ###########################################################
  3. #desc:后台守候检查服务是否正常
  4. #author:gaozhonghui
  5. #mail:toptreegzh@163.com
  6. #date:20121210
  7. ###########################################################
  8.  
  9. while true
  10. do
  11. /bin/sh /home/Gzh/shell/monitorTcp.sh > /dev/null 2>&1
  12. sleep 10
  13. done
#!/bin/sh
###########################################################
#desc:后台守候检查服务是否正常
#author:gaozhonghui
#mail:toptreegzh@163.com
#date:20121210
###########################################################

while true
do
   /bin/sh /home/Gzh/shell/monitorTcp.sh > /dev/null 2>&1
   sleep 10
done



monitorTcp.sh

Shell代码 复制代码 收藏代码
  1. #!/bin/sh
  2. #####################################################
  3. #desc:扫描后台服务器的应用服务器,若不能正常访问则重启
  4. #author:gaozhonghui
  5. #mail:toptreegzh@163.com
  6. #date:20121127
  7. #######################################################
  8.  
  9. year=`date -d "today" +"%Y"`
  10. monthday=`date -d "today" +"%m"`
  11. date=$(date -d "today" +"%Y%m%d")
  12.  
  13. #被监控服务器、端口列表
  14. #str = web服务文件夹:端口号
  15. server_all_list=(
  16. 'www.test2.com:9090'
  17. 'www.test.com:8090'
  18. )
  19.  
  20. #应用服务器基路径
  21. serverBasePath="/web/webserver/jboss/"
  22. #日志路径
  23. logBasePath="/web/webserver/logs/$year/$monthday/"
  24.  
  25. #获得监控服务PID
  26. function_getPID(){
  27. local PID=`ps -ef|grep $1|grep java |awk '{print $2}'`
  28. echo $PID
  29. }
  30.  
  31. #dump 线程详细信息方便定位问题
  32. function_jstackinfo(){
  33. PID=`function_getPID $1`
  34. if [ x$PID != x"" ] ;then
  35. if [ ! -d ${logBasePath} ];then
  36. mkdir -p ${logBasePath}
  37. fi
  38.  
  39. jstack -l ${PID} >> ${logBasePath}"jstack_$1_${date}.log"
  40. fi
  41. }
  42.  
  43. #关闭应用服务
  44. function_shutdown(){
  45. local shutdownSh=${serverBasePath}$1"/bin/shutdown.sh"
  46. if [ -f $shutdownSh ];then
  47. local PID=`function_getPID $1`
  48.  
  49. if [ x$PID != x"" ] ;then
  50. sh $shutdownSh > /dev/null 2>&1
  51. sleep 2
  52. fi
  53.  
  54. local PID2=`function_getPID $1`
  55.  
  56. if [ x$PID2 != x"" ];then
  57. kill -9 $PID2
  58. fi
  59. fi
  60. }
  61.  
  62. #启动应用服务
  63. function_startup(){
  64. local startupSh=${serverBasePath}$1"/bin/startup.sh"
  65. if [ -f $shutdownSh ];then
  66. sh $startupSh > /dev/null 2>&1
  67. fi
  68. }