目录
1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
要求:
1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
1.需要的服务sendmail和mailx
df常用参数 | 含义 |
-h | 使用易读方式输出 Filesystem Size Used Avail Use% Mounted on |
-H | 以1000为单位而不是1024 |
-k | 以KB为单位 Filesystem 1K-blocks Used Available Use% Mounted on |
-m | 以MB为单位 Filesystem 1M-blocks Used Available Use% Mounted on |
3.awk 'NR=行'{print $列}'表示逮你第几行第几列的内容
6.具体命令如下:
[root@redhat9 shellwenjian]# vim ade.sh
#!/bin/bash
mb=`df -m / | awk 'NR==2{print $4}'`
gb=$[mb/1024]
if [ "$gb" -lt 20 ]
then
echo "您最近的磁盘空间已经小于20G,请及时查看!" | mail -s "WORNING!" root@localhost
fi
[root@redhat9 shellwenjian]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
0 0 * * * root /shellwenjian/ade.sh
2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行,2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务并配置防火墙规则。
1.先判断是否下载http服务和firewall没有就安装,rpm -qa,yum install
3.通过进程和端口两种方式同时满足才能判断http运行正常,用到&&
#!/bin/bash
rpm_http=`rpm -qa httpd |wc -l`
if [ $rpm_http -ge 1 ]
then
systemctl start httpd;
else
yum install -y httpd;
systemctl start httpd;
fi
rpm_firewall=`rpm -qa firewalld |wc -l`
if [ $rpm_firewall -ge 1 ]
then
systemctl start firewalld;
firewall-cmd --add-service=http --permanent;
else
yum install -y firewalld;
systemctl start firewalld;
firewall-cmd --add-service=http --permanent;
fi
ps_ef=`ps -ef | grep "httpd" | wc -l`
port_web=`ss -lntup | grep -w "80" | wc -l`
if [ $ps_ef -ge 2 ] && [ $port_web -ge 2 ]
then
echo "web服务已运行!"
else
echo "稍后为您开启web服务!"
systemctl start httpd;
fi
##
#ps_ef=`ps -ef | grep "httpd" | grep -v grep | wc -l`
#port_web=`ss -lntup | grep -w "80" | grep -v grep | wc -l`
#if [ $ps_ef -ge 1 ] && [ $port_web -ge 1 ]
#then
# echo "web服务已运行!"
#else
# echo "稍后为您开启web服务!"
# systemctl start httpd;
#fi
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
1.将错误输出输出到/dev/null
具体命令如下:
#!bin/bash
curl 192.168.2.135 > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "web server is running!"
else
exit 12
fi
2.测试结果:
[root@redhat9 shellwenjian]# bash curl.sh
web server is running!
[root@redhat9 shellwenjian]# systemctl stop httpd.service
[root@redhat9 shellwenjian]# bash curl.sh
[root@redhat9 shellwenjian]# echo $?
12