前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux进程监控重启shell脚本

linux进程监控重启shell脚本

作者头像
bear_fish
发布2018-09-14 10:05:27
3.3K0
发布2018-09-14 10:05:27
举报

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1338385

本文主要内容:

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

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

相信监控重新是否运行,如果重新意外挂了,需要对重新重新启动这个场景应该十分常见吧,(下面是shell的log截图)

下面是shell脚本

下面的shell中test_process=”python.*nohup_restart_test_py” 之所以加上 yth… 是为了不kill shell的本身的grep程序,当然一般会加上 grep -v grep来排除掉本身shell的grep, 但是我觉得加上[]更方便点

代码语言:javascript
复制
#!/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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年08月30日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档