1. #!/bin/bash
    2. # 文件说明:监控应用程序运行,如已退出则启动应用程序
    3. export LANG=zh_CN.UTF-8
    4. IP=`/sbin/ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "`
    5. monitor_path=/root/monitor
    6. # 消息通知
    7. declare -a Emaillist
    8. Emaillist[0]=979243615@qq.com
    9. # 服务名
    10. declare -a app_name_list
    11. #服务启动文件
    12. declare -a app_path_list
    13. # 服务1:nginx
    14. app_name_list[0]=nginx
    15. app_path_list[0]='/usr/local/nginx/sbin/nginx'
    16. # 服务2:mongo
    17. app_name_list[1]=mongo
    18. app_path_list[1]='echo > /dev/null'
    19. # 服务3:redis
    20. app_name_list[2]=redis
    21. app_path_list[2]=redis
    22. # 服务4:ns平台
    23. app_name_list[3]=ns-platform-api-server
    24. app_path_list[3]='java -jar /usr/local/ns-platform-api-server-1.0-SNAPSHOT.jar'
    25. # 检测服务
    26. i=0
    27. for app_name in ${app_name_list[*]}
    28. do
    29. Count=`ps -aux | grep $app_name | wc -l`
    30. if [[ $Count -eq 1 ]]; then
    31. ${app_path_list[i]} > $monitor_path/service/$app_name.log 2>&1 &
    32. for email in ${Emaillist[*]}
    33. do
    34. #echo "服务器[$IP]: [$app_name]程序运行终止,现已启动该应用,请检查终止原因!" | mutt -s "服务器[$IP][$app_name]运行告警" $email
    35. echo 123 > /root/monitor/ch.log
    36. done
    37. echo "$(date "+%Y-%m-%d %H:%M:%S") [$app_name]服务器程序运行终止,现已启动该应用,请检查终止原因!" >> $monitor_path/monitor.log
    38. fi
    39. let i++
    40. done