实习的时候老大让写一个shell脚本,主要是监测一个程序的使用,因为用keepalived做HA,如果一台机器的那个端口为10000的应用程序down掉,则关掉keepalived对那台机器的检测,keepalived能够直接控制到另外一台机器
#根据端口号和进程号监控程序,这里是SharkServer2的服务,如果down掉,关闭该台机器的keepalived
#!/bin/bash
monitorLog=/home/zjw/monitor/monitor.log
time=`date +%Y-%m-%d%T`
echo "${time} begin check SharkServer2's PID....">>$monitorLog
pID=$(ps -ef|grep SharkServer2 |grep -v "grep"| awk '{print $2}')
echo "PID is: $pID">>$monitorLog
kill -0 $pID
sharkserver_exist_pid=$?
echo "sharkserver_exist_pid is: $sharkserver_exist_pid">>$monitorLog
if [ "$sharkserver_exist_pid" != 0 ]; then
service keepalived stop
echo "PID is not exist, Shark1 is stoped!">>$monitorLog
fi
# check sharkserver port
echo "${time} begin check SharkServer2' port....">>$monitorLog
pID2=$(netstat -nltp|grep 10000| awk '{print $7}')
sharkserver_exist_port=$?
echo "sharkserver_exist_port is: $sharkserver_exist_port">>$monitorLog
if [ "$sharkserver_exist_port" != 0 ]; then
service keepalived stop
echo "PORT is not exist, Shark1 is stoped!">>$monitorLog
fi
echo "------------------------------------">>$monitorLog