1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
第一步:创建脚本名为shell1.sh如下:
vim shell1.sh
第二步:做计划在shell1文件中,命令如下:
#!/bin/bash
test1=df -m | grep -w "/" | tr -s " " | cut -d " " -f4
str1=“您最近磁盘小于20G,请及时查看”
if [ “
t
e
s
t
1
"
−
l
t
20000
]
t
h
e
n
e
c
h
o
"
test1" -lt 20000 ] then echo "
test1"−lt20000]thenecho"str1” | mail -s “$str1” 3131579667@qq.com
fi
第三步:在邮箱文件中修改发送,并尝试发送文件
vim /etc/s-nail.rc
第三步:安装服务
yum install postfix s-nail
第四步:尝试给管理员邮箱发送
echo “test 1” |s-nail -s “title” 3131579667@qq.com
第五步:检测并尝试运行
给文件增加权限,并运行文件
chmod +x shell1.sh
./shell1.sh
2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
第一步:添加httpd服务
yum install httpd -y
第二步:创建脚本shell2.sh
vim shell2.sh
进程判断:
#!/bin/bash
num=$(ps -ef | grep httpd | grep -v grep | wc -l)
if [ $num -ge 1 ]
then
echo"httpd is running"
else
systemctl restart httpd
systemctl stop firewalld
fi
第三步:测试服务
systemctl stop httpd
sh shell2.sh
第四步:修改脚本中端口
端口判断:
#! /bin/bash
num=$(ps -ef | grep httpd | grep 80 | - wc -l )
if [ $num -ge 1 ]
then
echo"httpd is running!
else
systemctl restart httpd
systemctl stop firewalld
fi
第五步:测试服务
systemcti stop httpd
sh shell2.sh
sh shell2.sh
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
第一步:编写shell3.sh文件如下:
#!/bin/bash
curl -s 192.168.170.128 > /dev/null
if [[ $? = 0 ]]
then
echo " web server is runnning "
else
exit 12
fi
~
第二步:测试
vim shell3.sh
sh shell3.sh
echo $?