zoukankan      html  css  js  c++  java
  • shell 进程挂掉自动重启脚本

    一、注意看以下的命令的不同点

     

     二、python方式

    #!/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
    }
    
    ##脚本的$0 $1 $2 $3
    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

    三、shell方式

    #! /bin/bash
    
    while true 
    do
    	monitor=`ps -ef | grep Manipulator | grep -v grep | wc -l ` 
    	if [ $monitor -eq 0 ] 
    	then
    		echo "Manipulator program is not running, restart Manipulator"
    		./home/mk90/Documents/qt_exercise/build-Manipulator-Desktop-Debug/Manipulator #这里采用nohup programe & 2>&1 比较好
    	else
    		echo "Manipulator program is running"
    	fi
    	sleep 5
    done

    参考:比较全面

    https://lichangwei.github.io/2019/04/18/monitor-shell/

  • 相关阅读:
    WHMCS系统API调用
    Zend Guard Loader/Zend Loader是干什么的
    代理IP收集
    Jenkins 2.x版本的节点配置选项更新
    Visual Studio 2015 未响应/已停止工作的问题解决
    Visual Studio多版本进行切换的研究
    商城产品如何应对多个客户不同的需求修改并发布对应客户的文件
    Visual Studio插件
    微软注册dll在dotnet开发时起到缓存的作用
    Visual Studio 2015出现Cannot find one or more components. Please reinstall the application.的问题解决
  • 原文地址:https://www.cnblogs.com/weihua2020/p/13732778.html
Copyright © 2011-2022 走看看