#!/bin/sh #須要監測的服務列表 services=("common-server" "tomcat-server") startScript="" # 獲取服務啓動腳本 function getStart() { # 獨立服務啓動腳本 start="./$1/bin/start.sh" # tomcat服務啓動腳本 startup="./$1/bin/startup.sh" if [ -f $start ] then startScript=$start else startScript=$startup fi } while(true) do for s in ${services[@]}; do getStart $s sudo ps -ef | grep $s | grep -v "grep" >> /dev/null status=$? if [ $status -eq 0 ] then # 服務正常,無需啓動 echo $s" is ok." `date +%Y-%m-%d\ %H\:%M\:%S` >> /dev/null else # 服務中止,需重啓 echo "restart $s at " `date +%Y-%m-%d\ %H\:%M\:%S` >> monitor.log $startScript >> /dev/null sleep 2 fi done sleep 10 done