#!/bin/bash
# =====================================================================================================
# Name : reproc.sh
# Author : nanyun
# Version : v1.0
# Date : 20111220
# Copyright : XXXXXXXXXXXXXXXXXXXXX
# Description : 进程监控,需要监控的进程需要配置在"./proclist.txt"中,且必须为全路径。
# =====================================================================================================
ScriptSelfPwd=$(cd "$(dirname "$0")"; pwd)
cd "$ScriptSelfPwd"
CHECKFILE="TransRecord.txt"
CONFIGFILE="./proclist.txt"
MODIFYFILE="./config.ini"
#重启函数,参数为$1重启程序的全路径,$2为重启程序所在的目录
reproc()
{
cd "$2"
if [ "$PWD" != "$2" ];then
echo "PWD=$PWD
cd $2 failed."
return -1
fi
#修改下载的文件日期
datestring=$(grep 'GETDATE' "$MODIFYFILE"|awk -F= '{print $2}'|awk '{print $1}')
sed -i s/"$datestring"/`date +'%Y%m%d' -d '-1 day'`/g "$MODIFYFILE"
#启动程序
(source /oracle/.bash_profile && nohup "$1" &)
return 0
}
grep -v '^#' $CONFIGFILE |while read LINE
do
#检查要监控的程序文件是否存在
if [ ! -f "$LINE" ];then
echo -e "$LINE: No such file or directory."
continue
fi
psproc=$(ps -ef |grep "$LINE" |grep -v "grep")
DIRNAME=$(dirname "$LINE")
checkstring="find $DIRNAME -type f -mmin +100 -name $CHECKFILE"
if [ -z "$psproc" ];then
echo -e "`date +"%Y%m%d%H%M%S"` "$LINE" reboot."
reproc "$LINE" "$DIRNAME"
continue
elif (RUNCHECK=$($checkstring) && [ -n "$RUNCHECK" ]);then
echo -e "`date +"%Y%m%d%H%M%S"` "$LINE" kill and reboot."
#kill进程
killpid=$(ps -ef |grep "$LINE"|grep -v 'grep'|awk '{print $2}'|xargs kill -9)
reproc "$LINE" "$DIRNAME"
fi
done
exit 0