Linux 在shell脚本中解析命令的输出
我正在我的服务器上运行supervisord服务以运行两个二进制文件。从shell脚本中,我正在更新这两个脚本的RPM。当我更新它们时,我想检查它们是否正在运行。如果是,那么它会发出停止命令来停止二进制文件。如果不是,则不应打印错误消息。以下是我的示例输出和脚本:Linux 在shell脚本中解析命令的输出,linux,bash,shell,supervisord,Linux,Bash,Shell,Supervisord,我正在我的服务器上运行supervisord服务以运行两个二进制文件。从shell脚本中,我正在更新这两个脚本的RPM。当我更新它们时,我想检查它们是否正在运行。如果是,那么它会发出停止命令来停止二进制文件。如果不是,则不应打印错误消息。以下是我的示例输出和脚本: root>test-rc-002@/home/lab>supervisorctl status all Binary1 RUNNING pid 5444, upti
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$2等形式接收行。例如:
sub()
{如果[$2=“RUNNING”];则
回音跑
其他的
回显“未运行”
fi
}
运行asfasf的子asdf
子asdfa asdfasdf asfdasd