run_watch.sh
#!/bin/bash
SHELL_DIR=$(cd `dirname $0`; pwd)
cd ${SHELL_DIR}
CHECK_INTERVAL_SECONDS=1
if [ ! -e "watch_app.conf" ];then
echo "ERROR: run.conf is not exists"
exit 3
fi
PID_RET=""
IP_STR=`ifconfig eth0|grep inet|awk '{print $2}'`
source ./watch_app.conf
APP_MD5=`echo ${APP_NAME} | md5sum - | awk '{print $1}'`
SHELL_NAME=`basename $0`
get_watch_pid() {
PID_RET=`ps -ef | grep ${SHELL_NAME} | grep watch | grep ${APP_MD5} | awk '{print $2}'`
}
#获取本机内网ip
# getIp(){
# IP_STR = `ifconfig eth0|grep inet|awk '{print $2}'`
# echo "ip 地址为:$IP_STR重启了"
# }
#定义重启状态
RESTART_STATUS=""
#请求接口
sendMsg(){
RESULT=`curl --connect-timeout 10 -d "ip=${IP_STR}&status=${RESTART_STATUS}&sign=${SIGN}" --tlsv1 ${REQ_URL} --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"`
echo "请求结果为:$RESULT"
}
start() {
get_pid
if [ -z "${PID_RET}" ];then
start_proc
get_pid
if [ ! -z "${PID_RET}" ];then
echo "start ${APP_NAME} ok pid ${PID_RET}"
RESTART_STATUS="成功"
else
echo "start ${APP_NAME} fail"
RESTART_STATUS="失败"
return 1
fi
#看门狗启动freeswitch 发送一个请求
sendMsg
else
echo "${APP_NAME} is running pid ${PID_RET}"
fi
get_watch_pid
if [ -z "${PID_RET}" ];then
nohup sh ${SHELL_NAME} watch ${APP_MD5} 2>&1 1>/dev/null &
get_watch_pid
echo "start ${APP_NAME} watch ok pid ${PID_RET}"
else
echo "${APP_NAME} watch is running pid ${PID_RET}"
fi
}
stop() {
get_watch_pid
if [ ! -z "${PID_RET}" ];then
kill -9 ${PID_RET}
if [ ${?} -eq 0 ];then
echo "stop watch ${APP_NAME} pid ${PID_RET} ok"
else
echo "stop watch ${APP_NAME} pid ${PID_RET} fail"
return 1
fi
else
echo "${APP_NAME} watch is not running"
fi
get_pid
if [ ! -z "${PID_RET}" ];then
stop_proc ${PID_RET}
if [ ${?} -eq 0 ];then
echo "stop ${APP_NAME} pid ${PID_RET} ok"
else
echo "stop ${APP_NAME} pid ${PID_RET} fail"
return 1
fi
else
echo "${APP_NAME} is not running"
fi
}
get_status() {
get_pid
if [ ! -z "${PID_RET}" ];then
echo "${APP_NAME} is running pid ${PID_RET}"
else
echo "${APP_NAME} is not running"
fi
get_watch_pid
if [ ! -z ${PID_RET} ];then
echo "${APP_NAME} watch is running pid ${PID_RET}"
else
echo "${APP_NAME} watch is not running"
fi
}
watch() {
while :
do
sleep ${CHECK_INTERVAL_SECONDS}
get_pid
if [ -z "${PID_RET}" ];then
echo "${APP_NAME} is not running now start it"
start
fi
done
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
get_status
;;
watch)
if [ "$2" = "$APP_MD5" ];then
watch
fi
;;
*)
echo "Usage:sh $0 {start|stop|status|restart}"
exit 2
esac
配置文件:watch_app.conf
#实现启动进程接口
start_proc() {
/usr/local/freeswitch/bin/freeswitch -nc -waste -core -hp -vg -nonat
}
#实现返回进程PID接口,放到规定变量PID_RET
get_pid() {
PID_RET=`ps -ef | grep "freeswitch" | grep "nc" | awk '{print $2}'`
}
#实现结束进程接口,${1}为传入的进程号
stop_proc() {
kill -9 ${1}
}
#定义程序名,在同一个机器上必须唯一
APP_NAME="see_call_center"
REQ_URL="http://183.66.37.178:81/server/admin/mail/sendmail"
SIGN="one111"