题目:
作业一:
[root@server ~]# yum install s-nail -y
[root@server ~]# vim /etc/s-nail.rc
#定位最后一行添加
set from=2765709527@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=2765709527@qq.com
set smtp-auth-password=xangnmocigqfdddc
set smtp-auth=login
# 编写脚本
[root@server ~]# vim disk1.sh
#!/bin/bash
disk=`df -m | grep -w "/" |tr -s " " |cut -d " " -f4`
str1="warning disk space less than 20G!"
if [ "$disk" -lt 20000 ]
then
echo "$str1" | mail -s "$str1" 2765709527@qq.com
fi
# 编写周期性任务计划
[root@server ~]# vim /etc/crontab
0 0 * * * root /bin/bash /root/disk1.sh
[root@server ~]# bash disk1.sh
实验结果:
作业二:
# 编辑脚本
[root@server ~]# vim web1.sh
#!/bin/bash
ps=`ps -ef |grep "httpd" | grep -v "grep"|wc -l`
if [ "$ps" -gt 0 ]
then
echo "httpd is already running"
else
echo "httpd not started ,waiting...."
yum install httpd -y &> /dev/null
systemctl start httpd
systemctl start firewalld
firewall-cmd --permanent--zone=public --add-service=http > /dev/null
firewall-cmd--permanent--zone=public --add-port=80/tcp> /dev/null
firewall-cmd--reload > /dev/null
echo "httpd is already running!"
fi
[root@server ~]# bash web1.sh
httpd is already running
[root@server ~]# yum install httpd -y
上次元数据过期检查:0:01:22 前,执行于 2023年07月29日 星期六 22时03分07秒。
软件包 httpd-2.4.57-3.el9.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
作业三:
# 编写脚本实现功能
[root@server ~]# vim web2.sh
#!/bin/bash
curl -s 192.168.20.135 > /dev/null
if (($?==0))
then
echo "web server is running"
else
echo "web not accessible"
exit 12
fi
[root@server ~]# bash web2.sh
web server is running