题目
1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
首先,确保安装了邮件服务器(服务端安装命令:yum install postfix -y ;客户端安装命令:yum install mailx -y;开启邮件服务并开机自启:systemctl enable postfix --now)并确保邮件服务和crond服务正常运行
1、编写脚本
[root@localhost shell]# vim df_mail_crond.sh
#!/bin/bash
free_MB=`df -m / | awk '/\//{print $4}'`
free_GB=$[$free_MB/1024]
if [[ $free < 20480 ]] ;then
echo "Warning:The current disk space remaining is ${free_GB}G that less than 20G." | mail -s "Warning" root
else
echo "The current disk space remaining is ${free_GB}G.It is enough to use."
fi
2、设置权限
[root@localhost shell]# chmod a+rx df_mail_crond.sh
3、运行测试
[root@localhost shell]# ./df_mail_crond.sh
[root@localhost shell]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1 root Mon Jan 2 23:59 18/669 "Warning"
& 1
Message 1:
From root@localhost.localdomain Mon Jan 2 23:59:22 2023
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Date: Mon, 02 Jan 2023 23:59:22 +0800
To: root@localhost.localdomain
Subject: Warning
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root <root@localhost.localdomain>
Status: R
Warning:The current disk space remaining is 32G that less than 20G.
4、加入计划任务
[root@localhost shell]# vim /etc/crontab
0 0 * * * root /root/shell/df_mail_crond.sh
2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
首先确保安装httpd包(yum install -y httpd)
1、编写脚本
[root@localhost shell]# vim apache_start.sh
#!/bin/bash
ps_httpd=`ps -ef | grep httpd | grep -v grep | wc -l`
if [[ $ps_httpd > 0 ]];then
echo "httpd.service is running."
else
systemctl start httpd
systemctl stop firewalld
fi
2、设置权限
[root@localhost shell]# chmod a+rx apache_start.sh
3、运行测试
[root@localhost shell]# systemctl stop httpd
[root@localhost shell]# ./apache_start.sh
[root@localhost shell]# ./apache_start.sh
httpd.service is running.
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
1、编写脚本
[root@localhost shell]# vim web_test.sh
#!/bin/bash
curl -s 192.168.210.128 > /dev/null
if [[ $? = 0 ]];then
echo "web server is running."
else
exit 12
fi
2、设置权限
[root@localhost shell]# chmod a+rx web_test.sh
3、运行测试
[root@localhost shell]# ./web_test.sh
web server is running.
[root@localhost shell]# systemctl stop httpd
[root@localhost shell]# ./web_test.sh
[root@localhost shell]# echo $?
12