linux中自动检测服务脚本(apache举例)

 

1、系统

[root@centos7pc1 test]# lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.9.2009 (Core)
Release:        7.9.2009
Codename:       Core

 

2、安装httpd服务

[root@centos7pc1 test]# yum reinstall httpd -y

 

 

3、查看httpd服务状态

[root@centos7pc1 test]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd(8)
           man:apachectl(8)

May 05 10:26:30 centos7pc1 systemd[1]: Starting The Apache HTTP Server...
May 05 10:26:45 centos7pc1 httpd[3700]: AH00558: httpd: Could not reliably determine the server's fully qualified domain na...message
May 05 10:26:55 centos7pc1 systemd[1]: Started The Apache HTTP Server.
May 05 10:32:14 centos7pc1 systemd[1]: Stopping The Apache HTTP Server...
May 05 10:32:15 centos7pc1 systemd[1]: Stopped The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

 

4、开启服务并查看状态

[root@centos7pc1 test]# systemctl start httpd.service
[root@centos7pc1 test]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-05-05 10:38:14 CST; 4s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 4075 (httpd)
   Status: "Processing requests..."
    Tasks: 6
   CGroup: /system.slice/httpd.service
           ├─4075 /usr/sbin/httpd -DFOREGROUND
           ├─4084 /usr/sbin/httpd -DFOREGROUND
           ├─4085 /usr/sbin/httpd -DFOREGROUND
           ├─4086 /usr/sbin/httpd -DFOREGROUND
           ├─4087 /usr/sbin/httpd -DFOREGROUND
           └─4088 /usr/sbin/httpd -DFOREGROUND

May 05 10:37:48 centos7pc1 systemd[1]: Starting The Apache HTTP Server...
May 05 10:37:58 centos7pc1 httpd[4075]: AH00558: httpd: Could not reliably determine the server's fully qualified domain na...message
May 05 10:38:14 centos7pc1 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

 

5、关闭httpd服务并查看状态

[root@centos7pc1 test]# systemctl stop httpd.service
[root@centos7pc1 test]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd(8)
           man:apachectl(8)

May 05 10:26:30 centos7pc1 systemd[1]: Starting The Apache HTTP Server...
May 05 10:26:45 centos7pc1 httpd[3700]: AH00558: httpd: Could not reliably determine the server's fully qualified domain na...message
May 05 10:26:55 centos7pc1 systemd[1]: Started The Apache HTTP Server.
May 05 10:32:14 centos7pc1 systemd[1]: Stopping The Apache HTTP Server...
May 05 10:32:15 centos7pc1 systemd[1]: Stopped The Apache HTTP Server.
May 05 10:37:48 centos7pc1 systemd[1]: Starting The Apache HTTP Server...
May 05 10:37:58 centos7pc1 httpd[4075]: AH00558: httpd: Could not reliably determine the server's fully qualified domain na...message
May 05 10:38:14 centos7pc1 systemd[1]: Started The Apache HTTP Server.
May 05 10:39:22 centos7pc1 systemd[1]: Stopping The Apache HTTP Server...
May 05 10:39:23 centos7pc1 systemd[1]: Stopped The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

 

6、安装 nmap

[root@centos7pc1 test]# yum reinstall nmap -y

 

 

7、监测脚本及测试httpd关闭状态

[root@centos7pc1 test]# systemctl status httpd.service    ## 查看服务状态
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd(8)
           man:apachectl(8)

May 05 10:26:30 centos7pc1 systemd[1]: Starting The Apache HTTP Server...
May 05 10:26:45 centos7pc1 httpd[3700]: AH00558: httpd: Could not reliably determine the server's fully qualified domain na...message
May 05 10:26:55 centos7pc1 systemd[1]: Started The Apache HTTP Server.
May 05 10:32:14 centos7pc1 systemd[1]: Stopping The Apache HTTP Server...
May 05 10:32:15 centos7pc1 systemd[1]: Stopped The Apache HTTP Server.
May 05 10:37:48 centos7pc1 systemd[1]: Starting The Apache HTTP Server...
May 05 10:37:58 centos7pc1 httpd[4075]: AH00558: httpd: Could not reliably determine the server's fully qualified domain na...message
May 05 10:38:14 centos7pc1 systemd[1]: Started The Apache HTTP Server.
May 05 10:39:22 centos7pc1 systemd[1]: Stopping The Apache HTTP Server...
May 05 10:39:23 centos7pc1 systemd[1]: Stopped The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@centos7pc1 test]# cat monitor.sh             ## 查看监测脚本
#!/bin/bash
port=$(nmap -sT 192.168.65.153 | grep tcp | grep http | awk '{print $2}')        ## 此处的ip是自己的IP地址
if [ "$port" == "open" ]
    then
        echo "$(date) httpd is ok!" >> /tmp/autostart-acc.log
    else
        systemctl start httpd.service &> /dev/null
        echo "$(date) restart httpd!!" >> /tmp/autostart-err.log
fi
[root@centos7pc1 test]# bash monitor.sh          ## 运行监测脚本
[root@centos7pc1 test]# systemctl status httpd.service      ## 查看服务状态
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-05-05 10:49:25 CST; 5s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 4322 (httpd)
   Status: "Processing requests..."
    Tasks: 6
   CGroup: /system.slice/httpd.service
           ├─4322 /usr/sbin/httpd -DFOREGROUND
           ├─4331 /usr/sbin/httpd -DFOREGROUND
           ├─4332 /usr/sbin/httpd -DFOREGROUND
           ├─4333 /usr/sbin/httpd -DFOREGROUND
           ├─4334 /usr/sbin/httpd -DFOREGROUND
           └─4335 /usr/sbin/httpd -DFOREGROUND

May 05 10:48:59 centos7pc1 systemd[1]: Starting The Apache HTTP Server...
May 05 10:49:14 centos7pc1 httpd[4322]: AH00558: httpd: Could not reliably determine the server's fully qualified domain na...message
May 05 10:49:25 centos7pc1 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@centos7pc1 test]# cat /tmp/autostart-err.log     ## 查看日志文件
Thu May  5 10:49:25 CST 2022 restart httpd!!

 

8、测试开启状态

[root@centos7pc1 test]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-05-05 10:49:25 CST; 4min 45s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 4322 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
    Tasks: 6
   CGroup: /system.slice/httpd.service
           ├─4322 /usr/sbin/httpd -DFOREGROUND
           ├─4331 /usr/sbin/httpd -DFOREGROUND
           ├─4332 /usr/sbin/httpd -DFOREGROUND
           ├─4333 /usr/sbin/httpd -DFOREGROUND
           ├─4334 /usr/sbin/httpd -DFOREGROUND
           └─4335 /usr/sbin/httpd -DFOREGROUND

May 05 10:48:59 centos7pc1 systemd[1]: Starting The Apache HTTP Server...
May 05 10:49:14 centos7pc1 httpd[4322]: AH00558: httpd: Could not reliably determine the server's fully qualified domain na...message
May 05 10:49:25 centos7pc1 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@centos7pc1 test]# cat monitor.sh
#!/bin/bash
port=$(nmap -sT 192.168.65.153 | grep tcp | grep http | awk '{print $2}')
if [ "$port" == "open" ]
    then
        echo "$(date) httpd is ok!" >> /tmp/autostart-acc.log
    else
        systemctl start httpd.service &> /dev/null
        echo "$(date) restart httpd!!" >> /tmp/autostart-err.log
fi
[root@centos7pc1 test]# bash monitor.sh
[root@centos7pc1 test]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-05-05 10:49:25 CST; 5min ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 4322 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
    Tasks: 6
   CGroup: /system.slice/httpd.service
           ├─4322 /usr/sbin/httpd -DFOREGROUND
           ├─4331 /usr/sbin/httpd -DFOREGROUND
           ├─4332 /usr/sbin/httpd -DFOREGROUND
           ├─4333 /usr/sbin/httpd -DFOREGROUND
           ├─4334 /usr/sbin/httpd -DFOREGROUND
           └─4335 /usr/sbin/httpd -DFOREGROUND

May 05 10:48:59 centos7pc1 systemd[1]: Starting The Apache HTTP Server...
May 05 10:49:14 centos7pc1 httpd[4322]: AH00558: httpd: Could not reliably determine the server's fully qualified domain na...message
May 05 10:49:25 centos7pc1 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@centos7pc1 test]# cat /tmp/a
anaconda.log       autostart-acc.log  autostart-err.log
[root@centos7pc1 test]# cat /tmp/autostart-acc.log
Thu May  5 10:54:24 CST 2022 httpd is ok!

没有问题。

 

posted @ 2022-05-05 10:57  小鲨鱼2018  阅读(589)  评论(0编辑  收藏  举报