监控mysql服务是否开启:
#!/bin/bash
a=`ss -antlp | grep mysql|awk -F " " '{printf $4}'|sed 's/[^0-9]//g'` ##提取mysql的端口号码
b=` ps -ax |grep mysql | grep -v grep|wc -l`
##提取mysql的进程个数
if [[ $a -eq 3306 ]] && [[ $b -eq 2 ]]
then
printf "mysql is already running!\n"
else
printf "mysql not is running,and starting mysql!\n"
systemctl start mariadb.service
fi
远程监控apache服务是否开启
#!/bin/bash
a=`nmap 172.25.254.103 -p 80|grep open |wc -l`
if [[ $a -eq 1 ]]
then
printf "apache is already running!\n"
else
printf "apache not is running!\n"
printf "Apache Starting..."
fi
或者使用wget,curl通过url地址进行监控
[root@maillinux test]# wget -T 15 -q --spider http://172.25.254.13
[root@maillinux test]# echo $?
8
[root@maillinux test]# curl -s http://172.25.254.103 >/dev/null
[root@maillinux test]# echo $?
0
通过他们之中的任何一个返回值即可确定apache是否开启
本文介绍了一种通过shell脚本监控MySQL与Apache服务运行状态的方法,并提供了具体实现的脚本示例。对于MySQL,通过检查端口和进程数量来判断服务是否运行;对于Apache,则利用nmap和wget等工具来确认服务状态。

2010

被折叠的 条评论
为什么被折叠?



