向大家分享一个shell脚本的坑
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,打算在跳板机上写一个shell脚本,批量检查远程服务器上的main进程是否在健康运行中。先找出其中一台远程机器,查看main进程运行情况[root@two002 tmp]# ps -ef|grep m
千家信息网最后更新 2025年12月02日向大家分享一个shell脚本的坑
打算在跳板机上写一个shell脚本,批量检查远程服务器上的main进程是否在健康运行中。
先找出其中一台远程机器,查看main进程运行情况
[root@two002 tmp]# ps -ef|grep mainroot 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
shell检查脚本如下
[root@two002 tmp]# cat /tmp/main_check.sh#!/bin/bashNUM=$(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.shDon'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/bashNUM=$(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.shOh!My God! It's broken! main is stoped!
故在跳板机上,批量检查远程服务器的main进程运行状态的脚本为:
[root@tiaoban ~]# cat /usr/bin/main_check#!/bin/bashNUM=$(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>&1ssh -p22 root@$i "echo $i;sh /usr/bin/main_check"done
脚本
检查
结果
进程
运行
服务器
跳板
服务
精准
健康
内容
再次
参数
如上
情况
手动
机器
状态
过程
这是
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
华为云服务器凭证
申请软件开发的样本
数据库缓存有哪些技术
如何部署本地服务器
美女数据库图片
单位网络安全架设
山东省存储服务器测评
电脑网络安全微课ppt
残疾男子建无名尸数据库
刘剑广东软件开发公司
怎样搭建私人国外服务器
帆软实时数据库刷新
云计算网络安全专家
十大网络安全沙盒
顺昌盛世集团软件开发
宝塔面板数据库服务器在哪里
安康市网络安全宣传
淘宝的大数据库
绩溪租房软件开发
软件开发的基本条件
服务器的硬件如何管理
软件开发bug管理开源
数据库和统计源期刊的区别
机器人平台软件开发
北京林渠汇商网络技术
编程和网络安全的区别
我叫mt2为什么服务器下载失败
陕西诚信积分管理软件开发公司
设置数据库字段默认值
r星服务器密码格式