超详细shell脚本小练...

2024-02-21 00:04
文章标签 详细 shell 脚本 小练

本文主要是介绍超详细shell脚本小练...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

1.1.安装邮件服务

 

[root@node ~]# df -m
文件系统                   1M-块  已用  可用 已用% 挂载点
devtmpfs                     700     0   700    0% /dev
tmpfs                        716     0   716    0% /dev/shm
tmpfs                        287     8   279    3% /run
tmpfs                          4     0     4    0% /sys/fs/cgroup
/dev/mapper/openeuler-root 27385  2056 23913    8% /
tmpfs                        716     0   716    0% /tmp
/dev/sda1                    920    87   770   11% /boot
[root@node ~]# yum install mailx -y
[root@node ~]# vim /etc/mail.rc
set from=3471521632@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=3471521632@qq.com
set smtp-auth-password=anfbowpmrzfdchhi
set smtp-auth=login
注:使用:wq!强制退出

1.2.编写脚本

#!/bin/bash
disk=$(df -m | grep -w "/" | tr -s " " | cut -d " " -f4)
if [ $disk -lt 30000 ];thenecho "warning:disk space less than 30G!!!" | mail -s "warning:disk space less than 30G!!!" 3471521632@qq.com
fi注:df -m 以M为单位显示grep -w 精准定位tr -s 压缩空格

1.3.系统计划任务

0 0 * * *  root /bin/bash /root/df.sh

1.4.测试

发现时间不对 ,没有同步

[root@node ~]# timedatectl
System clock synchronized: no
              NTP service: active
          RTC in local TZ: no
[root@node ~]# systemctl start  chronyd
[root@node ~]# timedatectl
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

 再次测试


2、判断web服务是否运行

(1、査看进程的方式判断该程序是否运行2、通过查看端口的方式判断该程序是否运行)

如果没有运行,则启动该服务并配置防火墙规则。

2.1.编写脚本

(脚本名字不要含nginx,否则grep有误)

[root@server ~]# cat ng.sh
#!/bin/bash
ps=$(ps -ef | grep nginx | grep -v grep | wc -l)
#ps=$(netstat -lntup | grep -w 80 | wc -l)
#ps=$(ss -lntup | grep -w 80 | wc -l)
if [ "$ps" -eq 0 ]
thenecho "nginx is stopping, please waiting..."yum install nginx -y &> /dev/nullsystemctl start firewalldsystemctl start nginxfirewall-cmd --permanent --zone=public --add-server=http &> /dev/nullfirewall-cmd --permanent --zone=public --add-port=80 &> /dev/nullfirewall-cmd --reload > /dev/nullecho "nginx is running"
elseecho "nginx is already running..."
fi

 2.2.测试

[root@server ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
     Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disa
     Active: active (running) since Thu 2024-01-25 03:14:12 CST; 3mi
   

[root@server ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled
     Active: active (running) since Thu 2024-01-25 03:18:48 CST; 16s

3.用curl命令访问第二题的web服务,看能否正常访问,如果能正常访问,返回web-serveris running;如果不能正常访问,返回12状态码。

3.1.编写脚本

#!/bin/bash
ip=$(ip a | grep ens32 | grep inet | tr -s " " | cut -d " " -f3 | cut -d "/" -f1)
curl -s $ip &> $ip
if [ $? -eq 0 ]
then echo "web-server is running..."
elseecho "web is not running..."exit 12
fi

3.2.测试

 

这篇关于超详细shell脚本小练...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/729987

相关文章

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

Python3脚本实现Excel与TXT的智能转换

《Python3脚本实现Excel与TXT的智能转换》在数据处理的日常工作中,我们经常需要将Excel中的结构化数据转换为其他格式,本文将使用Python3实现Excel与TXT的智能转换,需要的可以... 目录场景应用:为什么需要这种转换技术解析:代码实现详解核心代码展示改进点说明实战演练:从Excel到

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元

SpringBoot整合easy-es的详细过程

《SpringBoot整合easy-es的详细过程》本文介绍了EasyES,一个基于Elasticsearch的ORM框架,旨在简化开发流程并提高效率,EasyES支持SpringBoot框架,并提供... 目录一、easy-es简介二、实现基于Spring Boot框架的应用程序代码1.添加相关依赖2.添

shell脚本自动删除30天以前的文件(最新推荐)

《shell脚本自动删除30天以前的文件(最新推荐)》该文章介绍了如何使用Shell脚本自动删除指定目录下30天以前的文件,并通过crontab设置定时任务,此外,还提供了如何使用Shell脚本删除E... 目录shell脚本自动删除30天以前的文件linux按照日期定时删除elasticsearch索引s

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

Goland debug失效详细解决步骤(合集)

《Golanddebug失效详细解决步骤(合集)》今天用Goland开发时,打断点,以debug方式运行,发现程序并没有断住,程序跳过了断点,直接运行结束,网上搜寻了大量文章,最后得以解决,特此在这... 目录Bug:Goland debug失效详细解决步骤【合集】情况一:Go或Goland架构不对情况二:

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详