监控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是否开启