linux如何让一个程序崩溃后自动重启(转)

  • 思路:  写一个脚本 监控程序的运行状态  没有运行启动运行 已运行不做操作。

    如果在控制台启动脚本 注意必须  nohup sh xxx.sh &

    while true
    do
        ps -ef | grep "填入你自己的程序名" | grep -v "grep"
        if [ "$?" -eq 1 ]
            then
            ./run.sh #启动应用,修改成自己的启动应用脚本或命令
            echo "process has been restarted!"
        else
            echo "process already started!"
        fi
        sleep 10
    done

    while :
    do
        cd /xxx
        ulimit -n 819200
        stillRunning=$(ps -ef |grep "填入你自己的程序名" |grep -v "grep")
        
        if [ "$stillRunning" ] ; then
            sleep 1
        else
            echo "starting  填入你自己的程序名 process ...."
            ./run.sh #启动应用,修改成自己的启动应用脚本或命令
            sleep 2
        fi
    done

posted on 2022-06-13 18:48  混元真人  阅读(961)  评论(0)    收藏  举报