Chinaunix首页 | 论坛 | 博客
  • 博客访问: 75167
  • 博文数量: 44
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2016-09-19 16:49
文章分类

全部博文(44)

文章存档

2018年(2)

2017年(1)

2016年(41)

我的朋友

分类: LINUX

2016-04-29 17:20:58

原文地址:守护进程shell 作者:gxhp1987

#!/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
阅读(808) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~