1.第一题
-
判断当前磁盘剩余空间是否有20G,如果小于20G,则将 报警邮件发送给管理员,每天检查一次磁盘剩余空间。**
第一步:安装邮件服务并配置
[root@server ~]# yum install s-nail -y
[root@server ~]# vim /etc/s-nail.rc
set from=2034795900@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=2034795900@qq.com
set smtp-auth-password=bqcisaibnhxtbaef
set smtp-auth=login
第二步:编写脚本
[root@server ~]# vim disk1.sh
#!/bin/bash
a=$(($( df | grep -w / | tr -s " " | cut -d " " -f4)/1024/1024))
if (($a >=20 ))
then
echo "磁盘剩余空间为$a G"
else
echo "磁盘剩余空间为$a G,小于20G"
echo "磁盘剩余空间为$a G,小于20G" |mail -s "磁盘剩余空间为$a G,小于20G" 2034795900@qq.com
fi
第三步:系统计划任务
[root@server ~]# chmod +x disk1.sh #设置执行权限
[root@server ~]# vim /etc/crontab
30 19 * * * root /bin/bash /root/disk1.sh
第四步:查看邮箱是否收到消息
2.第二题
-
判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则
法一:通过查看进程的方式判断该程序是否运行
第一步:编写脚本
[root@server ~]# vim web.sh
#!/bin/bash
ps=$(ps -ef | grep httpd |grep -v grep |wc -l)
if(($ps > 0))
then
echo "httpd is running"
else
echo "httpd not started"
yum install httpd -y > /dev/null
systemctl start httpd
systemctl start firewalld
firewall-cmd --permanent --zone=public --add-service=http > /dev/null
firewall-cmd --reload &> /dev/null
echo "http is already running"
fi
第二步:运行测试
[root@server ~]# vim web.sh
#!/bin/bash
ps=$(ps -ef | grep httpd |grep -v grep |wc -l)
if(($ps > 0))
then
echo "httpd is running"
else
echo "httpd not started"
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-service=80/tcp > /dev/null
firewall-cmd --reload &> /dev/null
echo "http is already running"
fi
法二:通过查看端口的方式判断该程序是否运行
第一步:恢复快照
第二步:编写脚本
[root@server ~]# vim web.sh
#!/bin/bash
ps=$(netstat -lntup | grep -w 80 | wc -l )
if(($ps > 0))
then
echo "httpd is running"
else
echo "httpd not started"
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 "http is already running"
fi
第三步:运行测试
[root@server ~]# bash web.sh
httpd not started
http is already running
[root@server ~]# bash web.sh
httpd is running
3.第三题
-
使用curl命令访问第二题的web服务,看能否正常访问, 如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码
第一步:编写脚本
[root@server ~]# vim judge.sh
#!/bin/bash
ip=$(ip a | grep ens160 | grep inet |cut -d / -f1 |tr -s ' ' | cut -d ' ' -f3)
curl -s $ip > /dev/null
if(($?==0))
then
echo "web server is running."
else
echo "web not accessible."
exit 12
fi
第二步:运行测试
[root@server ~]# bash judge.sh
web server is running.