shell监控pid进程的脚本

#require two params
#param 1:pid file path
#param 2:call back on pid process stop
ARGS=`getopt -o f:c: -al pidfile:,nostop: -- "$@"`
eval set -- "$ARGS"
while [ -n "$1" ]; do
  case "$1" in
    --pidfile) pidfile=$2; echo "$2";shift 2;;
    --nostop) nostop=$2; echo "$2";shift 2;;
    *)break;;
  esac
done

 
if [ ! $pidfile ] ;then
  echo "param1:pid file path is NULL";
  return;
fi

if [ ! $nostop ] ;then
  echo "param2:call back on pid process stop is NULl";
  return;
fi

echo -n "pid file in '$pidfile' ,call back on pid process stop is '$nostop',you sure?(y/n)"
read sure

if [ $sure != 'y' ] && [ $sure != 'Y' ] ; then
  echo "bye~~~"
  return;
fi

echo "start monitor"
log_file="./process.monitor.log"

while true 
do
  PID=$(cat $pidfile)
  echo -n "process PID is $PID"
  PID_EXIST=$(ps aux | awk '{print $2}'| grep -w $PID)

  echo "pid is exist: $PID_EXIST"
if [ "$PID_EXIST" != "$PID" ];then
  echo "`date +'%Y-%m-%d %H:%M:%S'`: the process $PID is not exist,will call back process" 
  bash -c "$nostop"
  echo "`date +'%Y-%m-%d %H:%M:%S'`: the process $PID is not exist,will call back process" >>$log_file
 else
  echo "`date +'%Y-%m-%d %H:%M:%S'`: the process $PID exist"
  echo "`date +'%Y-%m-%d %H:%M:%S'`: the process $PID exist" >> $log_file
 fi
 sleep 15m
done

 

posted @ 2020-09-09 15:12  panda's  阅读(520)  评论(0编辑  收藏  举报