Shell实现判断进程是否存在并重新启动脚本

本文介绍了一个用于监控特定进程并自动重启的bash脚本。该脚本通过检查进程是否存在来决定是否需要重启,适用于后台服务的维护。此外,还包含了一个更详细的示例,展示了如何监控并确保jboss服务正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简洁版:

    #! /bin/bash
    # author caoxin
    # time 2012-10-10 
    # program : 判断进行是否存在,并重新启动


    function check(){
      count=`ps -ef |grep $1 |grep -v "grep" |wc -l`
      #echo $count
      if [ 0 == $count ];then
        nohup python /runscript/working/$1 &
      fi
    }

    check behaviors.py

详细版:


    #!/bin/bash
    #

    #调用关闭jboss进程脚本
    stopMethodServer.sh

    #打印出当前的jboss进程:grep jboss查询的jboss进程,grep -v "grep" 去掉grep进程
    jmsThread=`ps -ef | grep gdms | grep jboss | grep -v "grep"`
    echo $jmsThread

    #查询jboss进程个数:wc -l 返回行数
    count=`ps -ef | grep gdms | grep jboss | grep -v "grep" | wc -l`
    echo $count

    sec=7
    #开始一个循环,以判断进程是否关闭

    for var in 1 2
    do
     if [ $count -gt 0 ]; then
      #若进程还未关闭,则脚本sleep几秒
      echo sleep $sec second the $var time, the JMS thread is still alive
      sleep $sec
     else
      #若进程已经关闭,则跳出循环
      echo "break"
      break
     fi
    done

    #if [ $count -eq 0 ]; then
    # echo "nohup startMethodServer.sh &"
    # nohup startMethodServer.sh &
    #else
    # echo "It's better to check the thread!!!"
    #fi

    #调用启动脚本
    nohup startMethodServer.sh &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高晓伟_Steven

相逢即是有缘,动力源于金钱。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值