在Shell脚本中解析命令的输出

时间:2018-07-30 11:02:33

标签: linux bash shell supervisord

我正在服务器上运行监督服务以运行两个二进制文件。从外壳脚本中,我正在更新两者的RPM。更新它们时,我想检查它们是否正在运行。如果是,则发出停止命令以停止二进制文件。如果不是,则错误消息不应打印。这是我的示例输出和脚本:

root>test-rc-002@/home/lab>supervisorctl status all
Binary1                            RUNNING   pid 5444, uptime 0:04:12
Binary2                            RUNNING   pid 5445, uptime 0:04:12

root>test-rc-002@/home/lab>service supervisord stop
Stopping supervisord (via systemctl):                      [  OK  ]

root>test-rc-002@/home/lab>supervisorctl status all
http://localhost:9001 refused connection

这是脚本的一部分:

supervisorctl stop Binary1
supervisorctl stop Binary2
service supervisord stop

PID=`ps -eaf | grep /opt/abcd/binary1/binary1 | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
  echo "killing $PID"
  kill -9 $PID
fi

我想做的是:

1)通过-supervisorctl status all检查状态

Binary1                            RUNNING   pid 5444, uptime 0:04:12
Binary2                            RUNNING   pid 5445, uptime 0:04:12 

2)如果它们正在运行,则只能传递命令-

supervisorctl stop Binary1
supervisorctl stop Binary2
service supervisord stop

3)如果它们没有运行,则“拒绝的连接”将出现在消息中的某个位置,将其接收,然后移至PID终止部分。该消息不应在终端上打印。

我是Shell脚本的新手,无法解析输出。请帮忙。

1 个答案:

答案 0 :(得分:0)

要解析文件中的行,您可以将每一行传递给另一个函数,该函数将以$ 1 $ 2等形式接收行。例如:

sub()
{     if [ $2 = "RUNNING" ]; then
        echo RUNNING
      else
          echo "not running"
     fi
}

sub asdf RUNNING asfasf
sub asdfa asdfasdf asfdasd
相关问题