向大家分享一个shell脚本的坑
打算在跳板机上写一个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脚本的坑的更多相关文章
- 分享一个shell脚本的坑:grep匹配+wc取值 在脚本执行后的结果与手动执行结果不一致
打算在跳板机上写一个shell脚本,批量检查远程服务器上的main进程是否在健康运行中. 先找出其中一台远程机器,查看main进程运行情况 [root@two002 tmp]# ps -ef|grep ...
- 分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)
分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...
- (转)分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)
分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...
- 分享一个SQLSERVER脚本
原文:分享一个SQLSERVER脚本 分享一个SQLSERVER脚本 很多时候我们都需要计算数据库中各个表的数据量很每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tablespac ...
- shell脚本中执行另一个shell脚本
分类: 可以在一个shell脚本中执行另一个shell脚本(或非可执行文件,主要用于取得一些变量的值),方法是: . 文件名(包括路径) 或 变量=文件名(包括路径) . $变量 注意,圆点后面有 ...
- 编写第一个 Shell 脚本
什么是 Shell 脚本? 一个 shell 脚本就是一个包含一系列命令的文件.shell 读取这个文件,然后执行 文件中的所有命令,就好像这些命令已经直接被输入到了命令行中一样. 怎样编写一个 Sh ...
- Shell教程 之第一个shell脚本
1.第一个shell脚本 打开文本编辑器(可以使用 vi/vim 命令来创建文件),新建一个文件 test.sh,扩展名为 sh(sh代表shell),扩展名并不影响脚本执行 输入一些代码 #!/bi ...
- 【下载】分享一个ida脚本,非常方便
标 题: [下载]分享一个ida脚本,非常方便作 者: 梁萧时 间: 2013-09-05,13:32:14链 接: http://bbs.pediy.com/showthread.php?t=178 ...
- 编写第一个Shell脚本【TLCL】
怎样编写一个 Shell 脚本 编写一个脚本 使脚本文件可执行 把脚本放到Shell能够找到的地方 脚本文件格式 #!/bin/bash # This is our first script. ech ...
随机推荐
- Elasticsearch 索引、更新、删除文档
一.Elasticsearch 索引(新建)一个文档的命令: curl XPUT ' http://localhost:9200/test_es_order_index/test_es_order_t ...
- MatLab Swap Rows or Cols 交换行或列
Matlab是矩阵运算的神器,所以可以很轻易的交换任意行或列,而且写法非常简洁,如下所示: a = [ ; ; ]; b = a; b(:,[;]) = b(:,[;]); % Swap col an ...
- SPOJ Distinct Substrings【后缀数组】
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- opengl导入obj模型
在经过查阅各种资料以及各种bug之后,终于成功的实现了导入基本的obj模型. 首相介绍一下什么是obj模型 一.什么是OBJ模型 obj文件实际上是一个文本文档,主要有以下数据,一般可以通过blend ...
- 各大互联网公司java开发面试常问问题
本人是做java开发的,这是我参加58,搜狐,搜狗,新浪微博,百度,腾讯文学,网易以及其他一些小的创业型公司的面试常被问的问题,当然有重复,弄清楚这些,相信面试会轻松许多. 1. junit用法,be ...
- Sorted sets
Redis in Action JOSIAH L. CARLSON MANNING Shelter Island ZSETs offer the ability to store a mapping ...
- Django - 补充目录
Django项目部署 importlib应用 - django contenttypes - django组件 Django - Model操作 Django - 用户认证.用户组.用户权限 Djan ...
- (四)Web应用开发---新增对象Master类别
UI设计类似如下形式 Copy如下Html代码到新创建的Html <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xh ...
- Qt计算器开发(三):执行效果及项目总结
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/guodongxiaren/article/details/26046543 执行效果 project ...
- Sql Server 2016 Always On集群搭建
第一步,配置好windows环境 第二步 (配置内容较多--需单写) 需要做windows集群 安装WSFC集群组件 直接在Windows服务器管理工具中,增加功能模块,集群故障转移模块 并增加节点 ...