linux进程监控重启shell脚本

本文主要内容:

  • shell日志date format
  • shell poll监控重新运行,挂了则重启程序

前段时间工作需要使用python+scrapy写了个新闻舆情爬虫系统,服务器上面跑了大概5个月,期间莫名其妙的挂了2~3次。最终决定使用shell一直监控指定的进程,如果没有运行,则重新启动重新。

相信监控重新是否运行,如果重新意外挂了,需要对重新重新启动这个场景应该十分常见吧,(下面是shell的log截图)
这里写图片描述
下面是shell脚本
下面的shell中test_process=”[p]ython.*nohup_restart_test_py” 之所以加上 [] [p]yth… 是为了不kill shell的本身的grep程序,当然一般会加上 grep -v grep来排除掉本身shell的grep, 但是我觉得加上[]更方便点

#!/bin/bash
#restart scrape news process if the process exited accidentally

log_file="restart_sh.log"

# return the current date time
TIMESTAMP(){
    echo $(date "+%Y-%m-%d %H:%M:%S")
}

stop_process_if_running(){
    # $1->process_name to grep
    echo $(ps -ef | grep $1)
    be_running=$(ps -ef | grep $1 | wc -l)
    if [ $be_running -gt 0 ]
    then
        echo "$(TIMESTAMP) $1 is running, T'm going to kill it"
        ps -ef | grep "$1" | awk '{print $2}' | xargs kill -9
        if [ $? -eq 0 ];
        then
            echo "kill $1 successfully!!!"
        fi
    else
        echo "$(TIMESTAMP) $1 is not running"
    fi
}

restart_process_if_die(){
    # $1->process_name by grep, $2->python directory
    # $3->process python file name
    echo "paras is: $@"
    be_running=$(ps -ef | grep $1 | wc -l)
    if [ $be_running -eq 0 ];
    then
        echo "$(TIMESTAMP) $3 got down, now I will restart it" | tee -a $log_file
        cd $2
        echo "Now I am in $PWD"
        nohup python $3 & 2>&1
        if [ $? -eq 0 ];
        then
            echo "$(TIMESTAMP) $3 restart successfully" | tee -a $log_file
        fi
        cd -
    else
        echo "$(TIMESTAMP) $3 is running, no need to restart"
    fi
}

test_process="[p]ython.*nohup_restart_test_py"
file_dir=/home/xiongyu/search_start_sh/
py_file=nohup_restart_test_py.py
#when execute this shell script, if the process is running,kill it firstly
stop_process_if_running $test_process

# poll if the process is died, if got died then restart it.
while :
do
    restart_process_if_die $test_process $file_dir $py_file
    echo "$(TIMESTAMP) now I will sleep 10S"
    sleep 10
done

你可能感兴趣的:(linux,soft,install,and,command)