Linux——Shell

if 语句

格式:if list; then list; elif list; then list; ... else list; fi

单分支

if 条件表达式; then

命令

fi

示例:

#!/bin/bash

N=10 if $N -gt 5 ; then

echo yes

fi

bash test.sh

yes

双分支

if 条件表达式; then

命令

else

命令

fi

示例

#!/bin/bash

N=10

if $N -lt 5 ; then

echo yes

else

echo no

fi

bash test.sh

no

多分支

if

条件表达式; then

命令

elif条件表达式; then

命令

else

命令

fi

示例

#!/bin/bash

N=$1

if $N -eq 3 ; then

echo "eq 3"

elif $N -eq 5 ; then

echo "eq 5"

elif $N -eq 8 ; then

echo "eq 8"

else

echo "no"

fi

实验

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

1)测试是否连接外网

root@localhost scripts# ping baidu.com

2)保证防火墙和selinux已关闭

root@localhost scripts# systemctl stop firewalld.service

root@localhost scripts# setenforce 0

3)安装sendmail和mailx

root@localhost \~# yum -y install sendmail mailx

4)启动

root@localhost \~# systemctl start sendmail

5)获取邮箱授权码

前往邮箱官网(POP3/SMTP/IMAP)

找到IMAP/SMTP,开启服务(需要验证码等)。收到授权码后,复制授权码

6)编辑/etc/mail.rc文件

root@localhost scripts# vim /etc/mail.rc

set from=15009281620@163.com #对方收到邮件时显示的发件人

set smtp=smtp.163.com #163邮箱地址

set smtp-auth-user=15009281620@163.com #邮箱账号

set smtp-auth-password=JCocvdssJMCO #授权密码

set smtp-auth=login #smtp的认证方式,默认是login,也可以改为CRAM---MD5或PLAIN方式

7)测试是否能发送消息

echo 内容|mail -s"主题"收件人

root@localhost scripts# echo "hello " | mail -s "hello" 15009281620@163.com

8)创建脚本文件

root@localhost scripts# vim Mem.sh #创建脚本文件

9)编辑脚本文件

脚本解析:

free -g | grep "Mem" | tr -s " " | cut -d" " -f 4

查看内存容量(-g)以G为单位查看,过滤"Mem"内存信息,去除空格,以空格为间隔,切除第四列信息。这就是我们需要的内存剩余容量
#!/bin/bash

##############################################################

File Name: free.sh

Version: V1.0

Author: Gao_XY

Email: Gao_XY@163.com

Organization:https://blog.csdn.net/ens33?type=blog

Created Time : 2024-12-12 22:29:59

Description:

##############################################################

a=`free -g | grep "Mem" | tr -s " " | cut -d" " -f 4` #定义变量,剩余容量为a变量

b=20 #定义需要比较的20为b变量

c="内存小于 $b G " #定义发送邮件的内容为c变量

if $a -le $b #if单分支,如果变量a小于变量b

then

echo $c | mail -s "内存警报" 15009281620@163.com #那么向邮箱发送邮件

fi

10)配置计划任务

root@localhost etc# crontab -e

0 0 */1 * * /bin/sh /scripts/Mem.sh #每天整点,执行Mem.sh文件

11)测试成功

2、判断web服务是否运行(如果没有运行,则启动该服务并配置防火墙规则。)

1、查看进程的方式判断该程序是否运行

1)创建脚本文件

root@localhost scripts# vim web_course.sh

2)编辑脚本文件

脚本解析:

ps -ef | grep nginx | grep -v grep | wc -l

查看进程 过滤nginx 过滤掉grep自身的进程 统计行数

其中的grep -v grep 是干啥的呢 ?

很简单 ,为了去除包含grep的进程行,避免影响最终数据的正确性
#!/bin/bash

##############################################################

File Name: web_course.sh

Version: V1.0

Author: Gao_XY

Email: Gao_XY@163.com

Organization:https://blog.csdn.net/ens33?type=blog

Created Time : 2024-12-13 01:53:33

Description:

##############################################################

course=` ps -ef | grep nginx | grep -v grep | wc -l` #将过滤统计出来的进程行号进行定义

if $course -gt 0 #如果进程的行号大于0

then

echo "nginx is running..." #那么该服务启动

else #否则启动该服务,并在防火墙放通该端口

firewall-cmd --permanent --zone=public --add-port=80/tcp &>/etc/null

systemctl start nginx

fi

3)测试

先停止web服务,执行脚本进行测试

查看web服务是否开启

root@localhost scripts]# systemctl is-active nginx.service

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

1)创建脚本文件

root@localhost scripts# vim web_port.sh

2)编辑脚本文件

脚本解析:

ss -lnupt | grep 80 &>/etc/null #查看80端口号,把查询结果送入黑洞

$? # 返回上一条命令是否执行成功,0 为执行成功,非 0 则为执行失败

echo $? #当执行该结果为0时,证明服务已启动

当服务没有启动的情况下,无法查询到80端口,"$?"就无法返回0。
#!/bin/bash

##############################################################

File Name: web.sh

Version: V1.0

Author: Gao_XY

Email: Gao_XY@163.com

Organization:https://blog.csdn.net/ens33?type=blog

Created Time : 2024-12-13 01:03:06

Description:

##############################################################

ss -lnupt | grep 80 &>/etc/null #查询80端口,将结果存入黑洞

port=`echo $?` #定义测试指令为port变量,测试上一条指令是否执行成功

if $port == 0 #如果结果为0

then

echo "nginx is running..." #那么执行成功

else #否则开启服务,并配置防火墙放行该端口

firewall-cmd --permanent --zone=public --add-port=80/tcp &>/etc/null

systemctl start nginx

fi

3)测试

查看服务是否开启

root@localhost scripts# systemctl is-active nginx.service

相关推荐
贝锐12 分钟前
金仓、达梦、TDSQL全适配:向日葵补齐信创数据库层远程运维能力
运维·数据库·信创
CodeYp20192 小时前
第14讲:mmap 实现文件 LRU 缓存
linux·lru·mmap
程序开发阿元2 小时前
一台 20G 的 VPS 被日志吃掉一半:我按这个顺序查了二十分钟
linux·ssh
AOI小白新手上路2 小时前
深度学习算力购置方案分析:本地 RTX 4090 服务器 vs 云端租赁
服务器·人工智能·深度学习
.冰块.2 小时前
流量大了自动加 Pod!K8s HPA 扩缩容与 ResourceQuota/LimitRange 资源管控实战
linux·云原生·容器·kubernetes·hpa·quota·limitrange
千桐科技3 小时前
qKnow智能体构建平台专业版 v3.1.5 发布:扩容工作流编排调用能力,上线Text2SQL插件,拓宽数据类智能应用构建边界
服务器·数据库·oracle·qknow
承渊政道3 小时前
Linux网络学习【深入理解多路转接select、poll的代码改写】
linux·ubuntu·select·多路转接·poll
雪落漂泊3 小时前
Linux进程(四)概念:命令行参数和环境变量篇
linux·运维·服务器
方便面不加香菜3 小时前
Docker Container -- 容器命令详解
运维·docker·容器
NextTech_3 小时前
不想学 vim,怎么改服务器上的配置文件?
服务器·编辑器·vim