1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
2、判断web服务是否运行(1、查看进程的方式判断该程序是否运行2、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务。
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。
[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、通过查看端口的方式判断该程序是否运行),如果没有运行,则启动该服务。
#!/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
测试
[root@redhat9 shellwenjian]# yum remove httpd firewalld
[root@redhat9 shellwenjian]# bash httpdfirewall.sh
Complete!
Warning: ALREADY_ENABLED: http
success
web服务已运行!
[root@redhat9 shellwenjian]# systemctl status httpd firewalld.service #状态
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2023-04-05 18:21:11 CST; 1min 7s ago
Docs: man:httpd.service(8)
Main PID: 58339 (httpd)
Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec: 0 B/sec"
Tasks: 213 (limit: 64977)
Memory: 33.8M
CPU: 104ms
CGroup: /system.slice/httpd.service
├─58339 /usr/sbin/httpd -DFOREGROUND
├─58341 /usr/sbin/httpd -DFOREGROUND
├─58342 /usr/sbin/httpd -DFOREGROUND
├─58343 /usr/sbin/httpd -DFOREGROUND
└─58476 /usr/sbin/httpd -DFOREGROUND
Apr 05 18:21:09 redhat9 systemd[1]: Starting The Apache HTTP Server...
Apr 05 18:21:11 redhat9 httpd[58339]: Server configured, listening on: port 443, port 80
Apr 05 18:21:11 redhat9 systemd[1]: Started The Apache HTTP Server.
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-04-05 18:21:14 CST; 1min 5s ago
Docs: man:firewalld(1)
Main PID: 59040 (firewalld)
Tasks: 2 (limit: 64977)
Memory: 26.0M
CPU: 357ms
CGroup: /system.slice/firewalld.service
└─59040 /usr/bin/python3 -s /usr/sbin/firewalld --nofork --nopid
Apr 05 18:21:14 redhat9 systemd[1]: Starting firewalld - dynamic firewall daemon...
Apr 05 18:21:14 redhat9 systemd[1]: Started firewalld - dynamic firewall daemon.
Apr 05 18:21:14 redhat9 firewalld[59040]: WARNING: ALREADY_ENABLED: http
[root@redhat9 shellwenjian]# firewall-cmd --list-services #firewalld
cockpit dhcpv6-client http ssh
[root@redhat9 shellwenjian]# curl 192.168.2.135 #httpd
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.openlab{
font-size: 66px;
color: red;
text-align: center;
}
</style>
</head>
<body>
<div class="openlab">welcome to openlab!!!</div>
</body>
</html>
3、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。
curl http://localhost &> /dev/null
if [ $? -eq 0 ];then
echo web server is running
else
exit 12fi