bash

Summary

  • 需要定时监控的程序,如果进程不存在就重启。

Demo

#!/bin/bash
# 检测程序进程,然后重新启动

# $1 程序目录
# $2 程序文件名


# 脚本日志文件位置
log_file="restart_sh.log"

# 获取时间戳函数
TIMESTAMP(){
    echo $(date "+%Y-%m-%d %H:%M:%S")
}

# 注意:grep 的结果需要剔除掉 grep 的进程和 check.sh 的进程。
be_running=`ps -aux | grep -w $2 | grep -v grep | grep -v check.sh | wc -l`

echo $be_running
echo $1
echo $2

if [ $be_running -eq 0 ];
then
    echo "$(TIMESTAMP) $2 got down, now I will restart it" | tee -a $log_file
    cd $1
    echo "Now I am in $PWD"
    java -jar $2 & 2>&1
    if [ $? -eq 0 ];
    then
        echo "$(TIMESTAMP) $2 restart successfully" | tee -a $log_file
    fi
    cd -
else
    echo "$(TIMESTAMP) $2 is running, no need to restart" | tee -a $log_file
fi
原文地址:https://www.cnblogs.com/duchaoqun/p/12751898.html