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

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

[root@two002 tmp]# ps -ef|grep main
root 23448 23422 0 11:40 pts/0 00:00:00 grep --color=auto main [root@two002 tmp]# ps -ef|grep main|grep -v grep|wc -l
0
shell检查脚本如下
[root@two002 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 执行脚本
[root@two002 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! [root@two002 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脚本内容修改如下:

[root@two002 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了
[root@two002 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! [root@two002 tmp]# sh /tmp/main_check.sh
Oh!My God! It's broken! main is stoped! 故在跳板机上,批量检查远程服务器的main进程运行状态的脚本为:
[root@tiaoban ~]# 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 [root@tiaoban ~]# 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 root@$i "echo $i;sh /usr/bin/main_check"
done  

向大家分享一个shell脚本的坑的更多相关文章

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

    打算在跳板机上写一个shell脚本,批量检查远程服务器上的main进程是否在健康运行中. 先找出其中一台远程机器,查看main进程运行情况 [root@two002 tmp]# ps -ef|grep ...

  2. 分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)

    分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...

  3. (转)分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)

    分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...

  4. 分享一个SQLSERVER脚本

    原文:分享一个SQLSERVER脚本 分享一个SQLSERVER脚本 很多时候我们都需要计算数据库中各个表的数据量很每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tablespac ...

  5. shell脚本中执行另一个shell脚本

    分类: 可以在一个shell脚本中执行另一个shell脚本(或非可执行文件,主要用于取得一些变量的值),方法是: . 文件名(包括路径) 或 变量=文件名(包括路径) . $变量   注意,圆点后面有 ...

  6. 编写第一个 Shell 脚本

    什么是 Shell 脚本? 一个 shell 脚本就是一个包含一系列命令的文件.shell 读取这个文件,然后执行 文件中的所有命令,就好像这些命令已经直接被输入到了命令行中一样. 怎样编写一个 Sh ...

  7. Shell教程 之第一个shell脚本

    1.第一个shell脚本 打开文本编辑器(可以使用 vi/vim 命令来创建文件),新建一个文件 test.sh,扩展名为 sh(sh代表shell),扩展名并不影响脚本执行 输入一些代码 #!/bi ...

  8. 【下载】分享一个ida脚本,非常方便

    标 题: [下载]分享一个ida脚本,非常方便作 者: 梁萧时 间: 2013-09-05,13:32:14链 接: http://bbs.pediy.com/showthread.php?t=178 ...

  9. 编写第一个Shell脚本【TLCL】

    怎样编写一个 Shell 脚本 编写一个脚本 使脚本文件可执行 把脚本放到Shell能够找到的地方 脚本文件格式 #!/bin/bash # This is our first script. ech ...

随机推荐

  1. postgresql----JSON类型和函数

    postgresql支持两种json数据类型:json和jsonb,而两者唯一的区别在于效率,json是对输入的完整拷贝,使用时再去解析,所以它会保留输入的空格,重复键以及顺序等.而jsonb是解析输 ...

  2. Laravel 中的异常处理

    这篇文章里,我们将研究 Laravel 框架中最重要也最少被讨论的功能 -- 异常处理. Laravel 自带了一个异常处理类,它能够让你以简单.优雅的方式 report 和 render 异常. 文 ...

  3. codeforces 782B - The Meeting Place Cannot Be Changed

    time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. is_file file_exists microtime performance

    对项目中旧代码的疑问 } elseif (substr($class_name, 0, 6) == 'OAuth2') { $file_name = $C->INCPATH . 'classes ...

  5. eclipse maven build、maven clean、maven install和maven test的区别 精析

          1.情景展示 选中maven项目,右键-->Run As或Debug As-->maven buid,maven install,maven test有什么区别? 2.区别说明 ...

  6. sql根据某一个字段重复只取第一条数据

    比如上图,取3,4行记录的第一行也就是3行,而不返回4行. 使用分析函数row_number() over (partiion by ... order by ...)来进行分组编号,然后取分组标号值 ...

  7. router-link params传参

    1.router.js配置 需要在路径后定义上要传的属性名 -->       /:属性名(query方式不需要) { path: '/CreateProgress/:name1', name: ...

  8. C++三大特性之继承

    原文地址:https://qunxinghu.github.io/2016/09/12/C++%20%E4%B8%89%E5%A4%A7%E7%89%B9%E6%80%A7%E4%B9%8B%E7%B ...

  9. FineReport实现java报表权限使用的效果图

    Java报表-多级权限配置说明 Java报表-联合填报 Java报表-模板内容权限控制 Java报表-权限细粒度控制

  10. MySQL Innodb日志机制深入分析

    MySQL Innodb日志机制深入分析 http://blog.csdn.net/yunhua_lee/article/details/6567869 1.1. Log & Checkpoi ...