0

我是 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),服务没有运行!

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

4

1 回答 1

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
于 2013-10-07T16:44:12.110 回答