Shell脚本 - 检查正在运行的服务

时间:2013-10-07 16:29:59

标签: bash awk

我是Shellscript的新手,我遇到了一些问题。我需要一个脚本来检查服务是否正在运行,如果它没有运行,并且不存在该标志,则启动所有服务。我做错了什么?

#!/bin/bash

file= "$PIN_HOME/apps/DE_BILL_MI_BRM/alarmistica/flags/intervencao.flag"

# Check if services are running 
for service in $BRM_SERVICES
do
    if  [ps -ef | grep $service | grep -v grep | awk 'NR>1{for (i=1;i<=NF;i++)if($i==1) print "Services not running", i}' ]; then
        echo $service " is not running correctly"
    else
        if [ -f "$file" ]; then
          echo "Flag exists. The service will not start"
        else
          echo "$file not found. Starting all services"
          pin_ctl start all
        fi      
    fi  
done

($i==1)时,服务没有运行!

但结果并不对应。例如,当服务停止时,脚本不启动服务......

1 个答案:

答案 0 :(得分:0)

要检查流程表,请改用pgrep

#!/bin/bash

file= "$PIN_HOME/apps/DE_BILL_MI_BRM/alarmistica/flags/intervencao.flag"

# Check if services are running 
for service in $BRM_SERVICES
do
   pgrep -f "$service";
   exstat=$?; # This checks the exit status
   if [ "$exstat" -eq 0 ] && ! [ -f "$file" ]; then
      echo "pgrep returned exit status $extstat";
   else
      echo "$file not found. Starting all services"
      pin_ctl start all
    fi       
done