加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

分享一个shell脚本的坑:grep匹配+wc取值 在脚本执行后的结果与

发布时间:2020-12-15 23:24:22 所属栏目:安全 来源:网络整理
导读:? 打算在跳板机上写一个shell脚本,批量检查远程服务器上的main进程是否在健康运行中。 先找出其中一台远程机器,查看main进程运行情况 [[email?protected] tmp]# ps -ef|grep mainroot 23448 23422 0 11:40 pts/0 00:00:00 grep --color=auto main[[email?p

?

打算在跳板机上写一个shell脚本,批量检查远程服务器上的main进程是否在健康运行中。

先找出其中一台远程机器,查看main进程运行情况

[[email?protected] tmp]# ps -ef|grep main
root     23448 23422  0 11:40 pts/0    00:00:00 grep --color=auto main

[[email?protected] tmp]# ps -ef|grep main|grep -v grep|wc -l
0

shell检查脚本如下

[[email?protected] tmp]# cat /tmp/main_check.sh 
#!/bin/bash
NUM=$(ps -ef|grep main|grep -v grep|wc -l)
if [ $NUM -eq 0 ];then
   echo "It‘s not good! main is stoped!"
else
   echo "Don‘t worry! main is running!"
fi

执行脚本

[[email?protected] tmp]# sh -x /tmp/main_check.sh
++ grep main
++ grep -v grep
++ wc -l
++ ps -ef
+ NUM=2
+ ‘[‘ 2 -eq 0 ‘]‘
+ echo ‘Don‘‘‘t worry! main is running!‘
Don‘t worry! main is running!

[[email?protected] tmp]# sh /tmp/main_check.sh
Don‘t worry! main is running!

如上执行结果,发现脚本执行过程中,看到赋予NUM参数的结果值是2但是手动执行ps -ef|grep main|grep -v grep|wc -l的结果明明是0!!
这是由于grep匹配的问题,需要grep进行精准匹配,即"grep -w"。这就需要将main_check.sh脚本内容修改如下

[[email?protected] tmp]# cat /tmp/main_check.sh 
#!/bin/bash
NUM=$(ps -ef|grep -w main|grep -v grep|wc -l)
if [ $NUM -eq 0 ];then
   echo "Oh!My God! It‘s broken! main is stoped!"
else
   echo "Don‘t worry! main is running!"
fi

再次执行检查脚本,就OK了

[[email?protected] tmp]# sh -x /tmp/main_check.sh
++ grep -w main
++ grep -v grep
++ wc -l
++ ps -ef
+ NUM=0
+ ‘[‘ 0 -eq 0 ‘]‘
+ echo ‘Oh!My God! It‘‘‘s broken! main is stoped!‘
Oh!My God! It‘s broken! main is stoped!

[[email?protected] tmp]# sh /tmp/main_check.sh
Oh!My God! It‘s broken! main is stoped!

故在跳板机上,批量检查远程服务器的main进程运行状态的脚本为:

[[email?protected] ~]# cat /usr/bin/main_check 
#!/bin/bash
NUM=$(ps -ef|grep -w main|grep -v grep|wc -l)
if [ $NUM -eq 0 ];then
   echo "Oh!My God! It‘s broken! main is stoped!"
else
   echo "Don‘t worry! main is running!"
fi

[[email?protected] ~]# cat /opt/script/main_check.sh 
#!/bin/bash

for i in $(cat /opt/ip.list)
do
/usr/bin/rsync -e "ssh -p22" -avpgolr /usr/bin/main_check $i:/usr/bin/ > /dev/null 2>&1
ssh -p22 [email?protected]$i "echo $i;sh /usr/bin/main_check"
done  

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读