1.判断当前磁盘剩余空间是否有20G,如果小于20G,则报警邮件发送给管理员,每天检查一次磁盘剩余空间
#打开硬盘所在目录
[root@server ~]# df ---硬盘所在目录(df -h 有单位 仅df 表示字节)
文件系统 1K-块 已用 可用 已用% 挂载点
devtmpfs 4096 0 4096 0% /dev
tmpfs 994720 0 994720 0% /dev/shm
tmpfs 397892 9560 388332 3% /run
/dev/mapper/rhel-root 16353280 5272912 11080368 33% /
/dev/nvme0n1p1 404128 249520 154608 62% /boot
tmpfs 198944 100 198844 1% /run/user/0
tmpfs 198944 36 198908 1% /run/user/1000
#检索硬盘根目录---[root@server ~]# df | grep /
devtmpfs 4096 0 4096 0% /dev
tmpfs 994720 0 994720 0% /dev/shm
tmpfs 397892 9552 388340 3% /run
/dev/mapper/rhel-root 16353280 5272908 11080372 33% /
/dev/nvme0n1p1 404128 249520 154608 62% /boot
tmpfs 198944 100 198844 1% /run/user/0
tmpfs 198944 36 198908 1% /run/user/1000
#精确匹配只包含根的(用- w参数表示只包含某个的)
[root@server ~]# df | grep -w /
/dev/mapper/rhel-root 16353280 5306740 11046540 33% /
#缩空格[root@server ~]# df | grep -w / | tr -s " "
/dev/mapper/rhel-root 16353280 5562808 10790472 35% /
#取第四个字符
[root@server ~]# df | grep -w / | tr -s " " | cut -d " " -f4
10636648
#安装邮件的软件
[root@server ~]# yum install s-nail -y
#编辑邮件的相关配置文件
[root@server ~]# vim /etc/s-nail.rc
set from=1506695644@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=1506695644@qq.com
set smtp-auth-password=邀请码
set smtp-auth=login
此处需要wq!强制退出
#编写脚本
[root@server ~]# vim lkh.sh
#!/bin/bash
l=$(df | grep -w / | tr -s " " | cut -d " " -f4)
if [ "$l" -lt 20000 ]
then
echo "剩余内存还有$l " | mail -s $l 1506695644@qq.com
fi
#编写周期性计划任务
[root@server ~]# vim /etc/crontab
* * * * * root /bin/bash /root/lkh.sh
2.判断web服务是否运行:1.查看进程的方式判断该程序是否运行 2.通过查看端口的方式判断该程序是否运行,如果没有运行,则启动该服务并配置防火墙规则
[root@server ~]# vim lkh1.sh
#!/bin/bash
yx=$(ps -ef | grep httpd | grep -v grep | wc -l)
if [ $yx -gt 0 ]
then
echo "httpd is already running"
else
echo "httpd is not running,waitting"
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 r
unning!!!"
fi
#查看进程的方式判断
[root@server ~]# bash lkh1.sh
httpd is already running
#查看端口号的方式判断
[root@server ~]# ss -lntup | grep -w 80 | wc -l
1
3.使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返沪web server is running,如果不能正常访问,返回12状态码
#提取IP地址
[root@server ~]# ip a | grep ens160 | grep inet | cut -d / -f1 | tr -s " " | cut -d " " -f3
192.168.217.128
[root@server ~]# vim lkh3.sh
#!/bin/bash
curl -s 192.168.217.128 &> /dev/null
if (($?==0))
then
echo "web server is running"
else
echo "web is not running"
exit 12
fi
[root@server ~]# bash lkh3.sh
web server is running