zoukankan      html  css  js  c++  java
  • linux下监视进程 崩溃挂掉后自动重启的shell脚本

     如何保证服务一直运行?如何保证即使服务挂掉了也能自动重启?在写服务程序时经常会碰到这样的问题。在Linux系统中,强大的shell就可以很灵活的处理这样的事务。

        下面的shell通过一个while-do循环,用ps -ef|grep 检查loader进程是否正在运行,如果没有运行,则启动,这样就保证了崩溃挂掉的进程重新被及时启动。

        必须注意两点:

        1、ps |grep 一个进程时必须加上其路劲,否则容易grep到错误的结果;

        2、必须用 -v 从结果中去除grep命令自身,否则结果非空。

        复制代码代码如下:

        #!/bin/sh

        while :

        do

        echo "Current DIR is " $PWD

        stillRunning=$(ps -ef |grep "$PWD/loader" |grep -v "grep")

        if [ "$stillRunning" ] ; then

        echo "TWS service was already started by another way"

        echo "Kill it and then startup by this shell, other wise this shell will loop out this message annoyingly"

        kill -9 $pidof $PWD/loader

        else

        echo "TWS service was not started"

        echo "Starting service ..."

        $PWD/loader

        echo "TWS service was exited!"

        fi

        sleep 10

        done

        如果启动此shell时发现进程已经存在,说明以别的方式启动了进程而不是此shell,那么它会持续提醒找到进程,解决办法是,要么只用此shell启动服务,要么一经发现以其他方式启动的服务即kill掉,上面的语句就是这么干的:

        kill -9 $pidof $PWD/loader

  • 相关阅读:
    第11组 Alpha冲刺(4/6)
    第11组 Alpha冲刺(3/6)
    第11组 Alpha冲刺(2/6)
    第11组 Alpha冲刺(1/6)
    团队Git现场编程实战
    第11组 团队项目-需求分析报告
    团队项目-选题报告
    第10组 Alpha冲刺(2/6)
    第10组 Alpha冲刺(1/6)
    2019 SDN上机第2次作业
  • 原文地址:https://www.cnblogs.com/lidabo/p/4231299.html
Copyright © 2011-2022 走看看