第七章 if条件语句的知识和实践邮件报警


范例7-2:

开发一个脚本判断系统剩余内存的大小,如果低于100MB,就邮件报警给系统管理员,并将脚本加入系统定时任务,即每3分钟执行一次。


首先搞定邮件服务,我的centos 7上面没有mail包,所以就下载一个rpm的mail包安装,下来时编辑配置文件

[root@localhost~]#tail-n7/etc/mail.rc

setfrom=***@163.com
setsmtp=smtp.163.com
setsmtp-auth-user=***
setsmtp-auth-password=***
setsmtp-auth=login
[root@localhost~]#

然后测试邮件服务器是否正常:
[root@localhost~]#echo"oldboy"|mail-s"cpu"***@163.com#好了
#!/bin/bash
men=`free-m|awk'NR==3{print$NF}'`
chars="currentmemoryis$men"

if[$men-lt3000];then
echo"$chars"|tee/tmp/messages.txt#打印并且保存在文件中
mail-s"`date+%F-%T`-men"17858655319@163.com</tmp/messages.txt
fi

范例 7-3:分别使用read读入及脚本传参的方式比较两个数字的大小。把前面涉及到的是否为整数以及传参的个数是否按照要求的。

#!/bin/bash
readnum
readnum1
if[-z"$num"];then
echo"plzinputcurrtarges"
exit4
fi
if[-z"$num1"];then#发现这两个不能放一起使用-a连接起来判断,会出现问题。
echo"plzinputcurrtarges"
exit5
fi

expr$num+4>/dev/null
aa=$?
expr$num1+4>/dev/null
bb=$?

if[$aa-eq0-a$bb-eq0];then
if[$num-lt$num1];then
echo""$num"<"$num1""
else
echo""$num">"$num1""
fi
else
echo"thisisabadint"
fi



范例:监控web和数据库的企业案例

用if条件语句针对Nginx Web服务或者MysqL数据库服务是否正常进行检测,如果服务未启动,则启动相应的服务;

#!/bin/bash

netstat-tlnup|grep3306>/dev/null
a=$?

char="theMysqLisaction"


if[$a-ne0];then
echo"theMysqLisdown"
systemctlstartmariadb
echo`netstat-tlnup|grep3306`
exit5
else[$a-eq0]
echo"$char"|tee/tmp/MysqL.txt
mail-s"`date+%F-%T`-MysqL"17858655319@163.com</tmp/MysqL.txt
fi

判断MysqL最佳的方式:
[root@localhost~]#netstat-tlnup|grep3306|wc-l
1

这是本地监控,如果是远程,可以使用telnet查看远程主机的MysqL的端口是不是存在。


客户端模拟用户访问的方式进行监控:

一般从web上访问MysqL的地址开发会给我们,所以我们可以使用wget或者curl

之前使用wget的方式:

wget --timeout 10 --tries=2 www.baidu.com> /dev/null


总结:本机检测端口使用nc,netstat,ps,远程主机使用telnet,客户端使用wget ,curl


范例:使用if条件语句比较两个整数的大小,使用传参的方法时,需要对传参个数及传入的参数是否为整数进行判断。

#!/bin/bash

a=$1
b=$2

if[-z$1];then
echo"thisisabadidea"
exit2
fi

if[-z$2];then
echo"thisisabad"
exit3
fi

expr$a+4>/dev/null
aa=$?
expr$b+3>/dev/null
bb=$?

if[$aa-eq0-a$bb-eq0];then

if[$a-lt$b]
then
echo""$a"<"$b""
elif[$a-gt$b]
then
echo""$a">"$b""
elif[$a-eq$b]
then
echo""$a"="$b""
else
echo"plztwocurretarge"
fi
fi

范例:判断字符串是否为数字的多种思路

[root@localhost~]#[-n"`echo"helloword"|sed"s/[0-9]//g"`"]&&echochar||echoint
char
[root@localhost~]#[-n"`echo"123"|sed"s/[0-9]//g"`"]&&echochar||echoint
int
[root@localhost~]#[-z`echo"helloword"|sed"s/[0-9]//g"`]&&echoint||echochar
char
[root@localhost~]#[-z`echo"2345"|sed"s/[0-9]//g"`]&&echoint||echochar
int
[root@localhost~]#[-z"`echo${a//[0-9]/}`"]&&echoint||echochar

范例:判断字符串长度是否为0的多种思路

[root@localhost~]#[-n"$a"]&&echoyes||echono
yes
[root@localhost~]#[!-z"$a"]&&echoyes||echono
yes
[root@localhost~]#["${#a}"-eq0]&&echoyes||echono
no

范例9:开发rsync的自动脚本

解题思路是先在物理机上安装一边rsync这个软件。看如何安装,执行什么命令启动

然后给脚本加上执行权限,命名为/etc/init.d/rsyncd


#!/bin/bash

if[-z$1];then
echo"plzonecanshu"
exit5
fi

if[$1=="start"];then
rsync--daemon
["`netstat-tlnup|greprsync|wc-l`"-gt0]&&echo"startingrsgnc..."||echo"nostart"
exit6
elif[$1=="stop"];then
#kill"`ps-ef|greprsync|grep-vgrep|awk-F'''{print$2}'`"
killallrsync
sleep2
["`netstat-tlnup|greprsync|wc-l`"-eq0]&&echo"stoprsync.."||echo"nostop"
exit3
elif[$1=="restart"];then
#kill"`ps-ef|greprsync|grep-vgrep|awk-F'''{print$2}'`"
kilallrsync
sleep2
rsync--daemon
["`netstat-tlnup|greprsync|wc-l`"-gt0]&&echo"starting"||echo"stop"

exit2
else
echo"plzcurrtint"
fi

如果要实现chkconfig来管理开机自动启动

给开头加上两行:

#chkconfig: 2345 20 80

#description: Saves and restores system entropy pool

第一行是说需要chkconfig管理,2345是linux的运行级别,表示该脚本认是在2345级别为启动状态,20是脚本的开始启动顺序,80是脚本的停止顺序,这两个数字都是不超过99的数字,一般情况下,可以根据服务的启动需求来选择,应用服务一般要考后启动为佳。

[root@localhost init.d]# vim rsyncd

[root@localhost init.d]# chkconfig --add rsyncd

[root@localhost init.d]# chkconfig --list rsyncd


注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。


要列出 systemd 服务,请执行 'systemctl list-unit-files'。

查看在具体 target 启用的服务请执行

'systemctl list-dependencies [target]'。


rsyncd 0:关 1:关 2:开 3:开 4:开 5:开 6:关

[root@localhost init.d]#

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...