shell第一次作业

该文描述了两个Linuxshell脚本,一是用于检查磁盘剩余空间,当低于20GB时发送警告邮件;二是确保Web服务(httpd)运行正常,包括安装、启动服务、防火墙配置。同时,还展示了使用curl命令验证Web服务的可访问性。
摘要由CSDN通过智能技术生成

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

1.需要的服务sendmail和mailx

2.用到的命令df

3.awk 'NR=行'{print $列}'表示逮你第几行第几列的内容 

4.mail -s 邮件标题 收件人

5.例行性任务crontab

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

二.判断web服务是否运行

1.先判断是否下载http服务和firewall没有就安装,rpm -qa,yum install

2.为httpd设置防火墙策略

3.通过进程和端口两种方式同时满足才能判断http运行正常,用到&&

4.再通过统计进程和端口信息条数来判断是否开启,wc -l

5.代码

#!/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

6.测试

[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>

三.使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

许逸仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值