当前位置: 首页 > news >正文

shell流程控制之条件判断练习

1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。​

因为如果磁盘剩余空间小于20G需要报警发送邮件给管理员,所以需要对管理员的邮箱进行设置

(1)首先安装软件:

[root@server scripts]# yum install -y s-nail

(2)进入邮件配置文件中设置管理员邮件:vim /etc/s-nail.rc

set from=1281984647@qq.com             
set smtp=smtp.qq.com                   
set smtp-quth-user=1281984647@qq.com   
set smtp-auth-password=dacqjvfivytdiagj
set smtp-auth=login  

(3)尝试向管理员发送邮件: echo "test 1" |s-nail -s "title" 1281984647@qq.com

编写shell脚本free.sh:将以下内容写入

if  [  "`df -h | grep /$ | tr -s " " | cut -d" " -f4 | cut -d"G" -f1`"  -lt 20 ]   
then                                                                               echo "管理员快看看你的内存不够20G了" | s-nail -s "title" 1281984647@qq.com
fi   

(4)测试运行:./free.sh

管理员成功的收到了内存不够的警告 !

(5)设置任务计划每天检查一次:crontab -e

0 0 * * * sh /server/scripts/free.sh
[root@server scripts]# crontab -l
0 0 * * * sh /server/scripts/free.sh

2、判断web服务是否运行

(1)查看进程的方式判断该程序是否运行

编写脚本:vim check_httpd.sh

if [  "`ps -aux |grep httpd |grep -v grep | tr -s " " |cut -d " " -f8 |uniq | wc -l`" -lt 2 ]
then                           systemctl start httpd      
else                           echo "httpd服务已经运行了"                                                    
fi 

测试:./check_httpd.sh

[root@server scripts]# ./check_web.sh 
[root@server scripts]# ./check_web.sh 
httpd服务已经运行了

(2)通过查看端口的方式判断该程序是否运行,如果没有运行,则启动该服务并配置防火墙规则。

编写脚本:vim check_httpd2.sh

if [ "`netstat -lnupt | grep 80 |wc -l`" -eq 0  ]                                  
then                                                                               systemctl start httpd;firewall-cmd --permanent --zone=public --add-port=80/tcp
else                                                                               echo "httpd服务已经启动"                                                       
fi

测试:./check_httpd2.sh

[root@server scripts]# ./check_web2.sh 
success
[root@server scripts]# ./check_web2.sh 
httpd服务已经启动

​3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。

编写脚本:vim curl.sh

read -p "请输入一个网址:" w
curl $w         
if  [ "`echo $?`" -eq 0  ]
then            echo "web server is running"
else            echo "状态码12"
fi   

测试:./curl.sh

[root@server scripts]# ./curl.sh 
请输入一个网址:www.baidu.com
web server is running[root@server scripts]# ./curl.sh 
请输入一个网址:www.qweqweweqweweeressd.com
curl: (6) Could not resolve host: www.qweqweweqweweeressd.com
状态码12
http://www.lryc.cn/news/64803.html

相关文章:

  • linux中TF启动卡制作:磁盘分区文件同步
  • 【操作系统OS】学习笔记:第一章 操作系统基础【哈工大李治军老师】
  • Linux C/C++ 网络编程中地址格式转换(inet_pton和inet_ntop函数)
  • 庖丁解牛函数知识---C语言《2》
  • Git 使用教程:最详细、最正宗手把手教学(万字长文)
  • 【华为OD机试 2023最新 】最优资源分配/芯片资源占用(C语言题解 100%)
  • markdown二元运算符
  • 【华为/华三】PPP
  • 【Java笔试强训 9】
  • 【C++】STL标准库之list
  • Nomogram | 盘点一下绘制列线图的几个R包!~(二)
  • Django之定时任务django-crontab
  • linux常见命令
  • 【14.HTML-移动端适配】
  • 平衡二叉树旋转机制
  • 深入浅出C++ ——C++11
  • 智能座舱3.0阶段,看全球巨头如何打造更具“价值”的第三空间
  • 【Linux】入门介绍
  • 【Python】序列类型②-元组
  • 循环的数字
  • MySQL查询之聚合函数查询
  • 普通2本,去过字节外包,到现在年薪25W+的测试开发,我的2年转行心酸经历...
  • util.callbackify
  • 解决使用CLIP模型时TypeError: Cannot handle this data type: (1, 1, 224, 224), |u1
  • Mysql第二章 多表查询的操作
  • ESP32-CAM:TinyML 图像分类——水果与蔬菜
  • 如何防止订单重复支付
  • 不是那么快乐的五一
  • Maven命令和配置详解
  • P3029 [USACO11NOV]Cow Lineup S 双指针 单调队列