#!/bin/bash
now=$(date '+%Y-%m-%d %H:%M:%S')
# the time interval between watchdog.sh monitorings
sleepTime=5
# log of watchdog.sh monitorings
watchdogLog="./watchdoglog.txt"
# current path
curDir=$(pwd)
# echo "current path is: \"$curDir\""
# the path of the monitored process
#baseDir="/home/admin/release/server/" # intranet
baseDir="/usr/local/src/ftp/release/server/" # extranet
if [[ $# -ne 1 && $# -ne 2 ]]; then
echo "$0 parameter number is error!"
exit
fi
# process name of watchdog.sh monitorings
procName=$1
echo "procName is: \"$procName\""
# parameter of process
paramName=$2
echo "paramName is: \"$paramName\""
user=$(whoami)
# echo "user is \"$user\""
if [[ "$user" != "root" ]]; then
echo "this tool must run as *root*"
exit
fi
while [[ 0 -lt 1 ]]
do
now=$(date '+%Y-%m-%d %H:%M:%S')
if [[ "$paramName" == "" ]]; then
ret=$(ps aux | grep "$procName$" | grep -v "grep" | grep -v "$0 $procName$" | wc -l)
else
ret=$(ps aux | grep "$procName $paramName$" | grep -v "grep" | grep -v "$0 $procName $paramName$" | wc -l)
fi
# echo "$procName $paramName ret = $ret"
if [[ $ret -lt 1 ]]; then
echo "$now process $procName $paramName is not exists..." >> "$watchdogLog"
# send email to notice administrator
echo "$now process $procName $paramName is not exists..." | mail -s "server status change notice" youremail@qq.com
cd "$baseDir"
# echo "./$procName $paramName"
if [[ "$paramName" == "" ]]; then
nohup ./"$procName" > /dev/null 2>&1 &
else
nohup ./"$procName" "$paramName" > /dev/null 2>&1 &
fi
cd "$curDir"
echo "$now restart process(pid=$!) $procName $paramName done..." >> "$watchdogLog"
fi
sleep $sleepTime
done