shell脚本 curl 和 wget访问 注册自启脚本 定时任务

curl检测项目是否启动脚本 

#!/bin/sh
# 获取中间件进程ID  /opt/AAS/domains
AASService=$(netstat -nlp | grep 16602 | awk '{print $7}' | awk -F"/" '{print $1}')  
# 中间件启动程序(中间件需要注意这里注意中间件实际安装的路径)  
StartAAS=/opt/AAS/domains/服务名/bin/startapusic
# 定义要监控的页面地址 
WebUrl=https://localhost:16602/ESS/heartBeat
# 日志输出
AASMonitorLog=/opt/AAS/domains/服务名/logs/AASMonitor.log
 echo "----------------开始--------------"  >> ${AASMonitorLog}
echo "检测Start" >> ${AASMonitorLog}
 if [[ $AASService ]];then
	echo "AASService="${AASService} >> ${AASMonitorLog}
	else
	echo "未启动">> ${AASMonitorLog}
 fi
echo "WebUrl="${WebUrl} >> ${AASMonitorLog}
Monitor()
{  
  echo "[info]开始监控中间件...[$(date +'%F %H:%M:%S')]"  
  if [[ $AASService ]];then # 这里判断中间件进程是否存在  
    echo "[info]当前中间件进程ID为:$AASService,继续检测页面..."  
    # 检测是否启动成功(成功的话页面会返回状态"200")  
	prefix=${WebUrl:0:5}
	sleep 30
	if [ $prefix == https ];then
		echo "https请求"
		AASServiceCode=$(curl -k --tlsv1 -m 10 -o /dev/null -s -w %{http_code} $WebUrl) 
	else
		echo "http请求"
    	AASServiceCode=$(curl -I -m 10 -o /dev/null -s -w %{http_code} $WebUrl)  
	fi
	echo "链接返回请求结果$AASServiceCode"
    if [ $AASServiceCode == 200 ];then  
        echo "[info]页面返回码为$AASServiceCode,中间件启动成功,测试页面正常......"  
		echo "--------------结束----------------"
    else  
        echo "[error]中间件页面出错,请注意......状态码为$AASServiceCode,错误日志已输出到$AASMonitorLog"  
        echo "[error]页面访问出错,开始重启中间件"  
        kill -9 $AASService  # 杀掉原中间件进程  
        sleep 30   
		echo "--------------结束----------------"  
        $StartAAS>/dev/null 2>&1
		
    fi  
  else  
    echo "[info]中间件进程不存在!中间件开始自动重启..."  
    echo "[info]执行 $StartAAS,请稍候......" 
	echo "--------------结束----------------"   
     $StartAAS>/dev/null 2>&1
	
  fi  
	sleep 30
  
	
}  

Monitor>>$AASMonitorLog

 

wget检测服务启动

#!/bin/sh
# 获取中间件进程ID  /opt/AAS/domains
AASService=$(netstat -nlp | grep 16602 | awk '{print $7}' | awk -F"/" '{print $1}')  
# 中间件启动程序(中间件需要注意这里注意中间件实际安装的路径)  
StartAAS=/opt/AAS/domains/服务名/bin/startapusic
# 定义要监控的页面地址 
WebUrl=https://localhost:16602/heartBeat
# 日志输出
AASMonitorLog=/opt/AAS/domains/服务名/logs/AASMonitor.log 
 echo "----------------开始--------------"  >> ${AASMonitorLog}
echo "检测Start" >> ${AASMonitorLog}
 if [[ $AASService ]];then
	echo "AASService="${AASService} >> ${AASMonitorLog}
	else
	echo "未启动">> ${AASMonitorLog}
 fi
echo "WebUrl="${WebUrl} >> ${AASMonitorLog}
Monitor()
{  
  echo "[info]开始监控中间件...[$(date +'%F %H:%M:%S')]"  
  if [[ $AASService ]];then # 这里判断中间件进程是否存在  
    echo "[info]当前中间件进程ID为:$AASService,继续检测页面..."  
	sleep 60
 	if wget --no-check-certificate --spider -q -o /dev/null --tries=1 $WebUrl;then
		echo "[info]请求心跳机制成功返回true,中间件启动成功,测试页面正常......"  
		echo "--------------结束----------------"
	else
		echo "[error]请求心跳机制失败返回false,错误日志已输出到$AASMonitorLog"  
        echo "[error]页面访问出错,开始重启中间件"  
        kill -9 $AASService  # 杀掉原中间件进程  
        sleep 15 
		echo "--------------结束----------------"  
        $StartAAS>/dev/null 2>&1
	fi
  else  
    echo "[info]中间件进程不存在!中间件开始自动重启..."  
    echo "[info]执行 $StartAAS,请稍候......" 
	echo "--------------结束----------------"  
	sleep 5 
    $StartAAS>/dev/null 2>&1
  fi  
}  

Monitor>>$AASMonitorLog

 

开机自启注册服务脚本

[Unit]
Description=服务名 service
After=network.target remote-fs.target nss-lookup.target
[Service]
ExecStart=/opt/AAS/domains/服务名/bin/startapusic
ExecStop=kill -9 ${pidof 服务名}
ExecReload=kill-9 ${pidof 服务名} && /opt/AAS/domains/服务名erver/bin/startapusic

[Install]
WantedBy=multi-user.target


# 放到/usr/lib/systemd/system/ 或 /etc/systemd/system下
# systemctl daemon-reload 让系统重新读取服务文件
# systemctl enable 服务名 系统自启动
# service 服务名 start / stop / restart

写入定时任务脚本 需要使用root用户执行

#!/bin/sh

scriptAddr=$(pwd)/ApplyMonitor.sh
AASPath=/var/cronScript
cronPath=/var/spool/cron/crontabs
if [ ! -d $AASPath ];then	
	echo "mkdir $AASPath"
	mkdir -p $AASPath
fi

if [ ! -f $AASPath/ApplyMonitor.sh ];then
	echo "不存在ApplyMonitor.sh 复制$scriptAddr 到 $AASPath目录下"
	cp $scriptAddr $AASPath
	chmod a+w $AASPath/ApplyMonitor.sh
		if [ -f $cronPath/root ]; then	
			echo "存在$cronPath/root"
		else
			echo "不存在$cronPath/root 创建定时任务文档"
			touch $cronPath/root
		fi
crontab -l >> conf && echo "*/10 * * * * $AASPath/ApplyMonitor.sh" >> conf && crontab conf && rm -f conf
fi 
echo "=====结束====="


你可能感兴趣的:(Linux,linux,运维)