Linux Centos7通过shell脚本来监控mysql的运行状态

vim checkmysql.sh

#!/bin/sh
#create by mingongge at 2018-10-10
port=`netstat -lnt|grep 3306|wc -l`

if  [ $post -ne 1 ] ;then
   nowtimes=$(date"+%Y%m%d-%H%M%S")
   echo "\nmysql is stop time:"$nowtimes >> ./checkmysql.txt
   systemctl restart mtsqld
else
   echo "mysql is running"
fi
 

ctrl+c  wq! 保存退出

注意 这个时候在服务器上运行脚本  sh checkmysql.sh  

会报错。

百度了一下原因,这个是因为sh脚本里面的if这边的规则。

 [ ] 两边的中括号前后都要是空格

shell的关键字和格式真蛋疼

修改完这个后继续运行  会报

扫描二维码关注公众号,回复: 3619154 查看本文章

这个是因为shell脚本里面  只有整数才能用-ne  eq 这种做比较  字符串建议使用 = 号来做判断

注意 shell脚本使用变量赋值成脚本命令  需要使用反引号

小白需要注意这种细节!

猜你喜欢

转载自blog.csdn.net/weixin_42135441/article/details/82993085