[英]Parse output of a command in shell script
我在服务器上运行监督服务以运行两个二进制文件。 从外壳脚本中,我正在更新两者的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全部检查状态
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()
{ if [ $2 = "RUNNING" ]; then
echo RUNNING
else
echo "not running"
fi
}
sub asdf RUNNING asfasf
sub asdfa asdfasdf asfdasd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.