超详细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

相关文章

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Oracle数据库定时备份脚本方式(Linux)

《Oracle数据库定时备份脚本方式(Linux)》文章介绍Oracle数据库自动备份方案,包含主机备份传输与备机解压导入流程,强调需提前全量删除原库数据避免报错,并需配置无密传输、定时任务及验证脚本... 目录说明主机脚本备机上自动导库脚本整个自动备份oracle数据库的过程(建议全程用root用户)总结

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

2025版mysql8.0.41 winx64 手动安装详细教程

《2025版mysql8.0.41winx64手动安装详细教程》本文指导Windows系统下MySQL安装配置,包含解压、设置环境变量、my.ini配置、初始化密码获取、服务安装与手动启动等步骤,... 目录一、下载安装包二、配置环境变量三、安装配置四、启动 mysql 服务,修改密码一、下载安装包安装地

在macOS上安装jenv管理JDK版本的详细步骤

《在macOS上安装jenv管理JDK版本的详细步骤》jEnv是一个命令行工具,正如它的官网所宣称的那样,它是来让你忘记怎么配置JAVA_HOME环境变量的神队友,:本文主要介绍在macOS上安装... 目录前言安装 jenv添加 JDK 版本到 jenv切换 JDK 版本总结前言China编程在开发 Java

Spring Boot Actuator应用监控与管理的详细步骤

《SpringBootActuator应用监控与管理的详细步骤》SpringBootActuator是SpringBoot的监控工具,提供健康检查、性能指标、日志管理等核心功能,支持自定义和扩展端... 目录一、 Spring Boot Actuator 概述二、 集成 Spring Boot Actuat

linux下shell脚本启动jar包实现过程

《linux下shell脚本启动jar包实现过程》确保APP_NAME和LOG_FILE位于目录内,首次启动前需手动创建log文件夹,否则报错,此为个人经验,供参考,欢迎支持脚本之家... 目录linux下shell脚本启动jar包样例1样例2总结linux下shell脚本启动jar包样例1#!/bin

如何在Java Spring实现异步执行(详细篇)

《如何在JavaSpring实现异步执行(详细篇)》Spring框架通过@Async、Executor等实现异步执行,提升系统性能与响应速度,支持自定义线程池管理并发,本文给大家介绍如何在Sprin... 目录前言1. 使用 @Async 实现异步执行1.1 启用异步执行支持1.2 创建异步方法1.3 调用

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三