shell

一、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。

二、判断web服务是否运行

三、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。


一、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。

        1.服务端和客户端安装mailx和postfix

[root@A ~]# yum install postfix -y
[root@A ~]# systemctl enable postfix --now

[root@B ~]# yum install sendmail -y

        2.编写脚本

set from=2151208022@qq.com             
set smtp=smtp.qq.com                   
set smtp-quth-user=2151208022@qq.com   
set smtp-auth-password=QMSWZJIRXHTVILLU
set smtp-auth=login  

         3.判断当前磁盘剩余空间是否由20G

a=`df -m | grep -w /boot | tr -s " " " "|cut -d " " -f 4`
b=`df -m | grep -w / | tr -s " " " "|cut -d " " -f 4`
c=$((a+b))
free=$((c/1024))
if [ $free -lt 20 ]
then
  echo 空间不足!| mail -s "内存报警!" redhat
fi

       4.设置任务计划 

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 /root/mail.sh

二、判断web服务是否运行

        1.下载httpd包

[root@A~]# yum install httpd -y

        2.编写脚本

ps_httpd=`ps -ef | grep httpd | grep -v grep | wc -l`
if [[ $ps_httpd > 0 ]];then
  echo "httpd.server is runing"
else
  systemctl start httpd
  systemctl stop firewalld
fi                                      

        3.测试

[root@A ~]# bash httpd.sh 
httpd.server is runing

三、使用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,则返回web server is running;如果不能正常访问,返回12状态码。

        1.编写脚本

curl -s 192.168.90.132 > /dev/null
if [[ $? = 0 ]];then
  echo "web server is runing"
else
  exit 10
fi

        2.测试

[root@A ~]# systemctl restart httpd
[root@A ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; ven>
     Active: active (running) since Sun 2023-05-28 12:40:28 CST; 8s ago
       Docs: man:httpd.service(8)
   Main PID: 5895 (httpd)
     Status: "Started, listening on: port 80"
      Tasks: 213 (limit: 10756)
     Memory: 47.9M
        CPU: 82ms
     CGroup: /system.slice/httpd.service
             ├─5895 /usr/sbin/httpd -DFOREGROUND
             ├─5896 /usr/sbin/httpd -DFOREGROUND
             ├─5897 /usr/sbin/httpd -DFOREGROUND
             ├─5898 /usr/sbin/httpd -DFOREGROUND
             └─5899 /usr/sbin/httpd -DFOREGROUND
 
5月 28 12:40:28 server.hostname.com systemd[1]: Starting The Apache HTTP >
[root@server ~]# bash web.sh 
web server is runing
[root@server ~]# systemctl stop httpd
[root@server ~]# bash web.sh 
[root@server ~]# echo $?
10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值