解决Tomcat因Redis加载慢而启动失败的问题
StartTomcat.py
import subprocess as t
import time, os, requests, sys WEB_IP = '127.0.0.1:8080'
# WEB_IP = '127.0.0.1'
REDIS_IP = '127.0.0.1'
REDIS_PORT = '' # 获取当前时间
def getCurrentTime():
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) # 检查 Tomcat
def check_Tomcat():
var = t.getoutput("ps -ef | grep tomcat")
success = False
for line in var.split("\n"):
if '/usr/local/tomcat7/conf/logging.properties' in line:
success = True if not success:
return False # 判断是不是 Tomcat僵死掉
try:
requests.get("http://" + WEB_IP + "/dsideal_yy/adminlogin/captchaText", timeout=1)
except requests.exceptions.ConnectTimeout:
return False
except requests.exceptions.Timeout:
return False # 判断验证码是不是能正常生成?
r = requests.get("http://" + WEB_IP + "/dsideal_yy/adminlogin/captchaText")
if r.status_code != 200:
return False
else:
# 下载图片
try:
r = requests.get("http://" + WEB_IP + "/dsideal_yy/adminlogin/captchaImage?text=" + r.json()['text'])
with open('/usr/local/software/picture.jpg', 'wb') as file:
file.write(r.content)
# 用完就删除了吧
os.remove('/usr/local/software/picture.jpg')
return True
except Exception as err:
print('Tomcat检查发现异常:' + str(err))
return False # 重新启动Tomcat
def RestartTomcat(): # 准备了十秒的缓冲时间,用来将mysql和ssdb进行启动应该足够了
# 开始关闭Tomcat
cmd = '/usr/local/tomcat7/bin/shutdown.sh'
os.system(cmd)
print('正在关闭Tomcat...')
# 休息5秒
cmd = "sleep 5"
os.system(cmd) # 查找Tomcat进程并杀死进程
cmd = "ps -ef | grep 'tomcat' | grep -v grep| awk '{print $2}' | xargs kill -9"
os.system(cmd)
print('正在杀掉Tomcat...')
# 休息5秒
cmd = "sleep 5"
os.system(cmd) # 重新启动Tomcat
cmd = '/usr/local/tomcat7/bin/startup.sh'
os.system(cmd) while True:
r = check_Tomcat()
if r:
print('恭喜!Tomcat启动成功!')
break
else:
time.sleep(1)
print('Tomcat还在启动中,请稍等...' + getCurrentTime()) if __name__ == "__main__": # 创建工作目录
path = '/usr/local/software/CloudPlatformUtil/AutoDeployment/'
if not os.path.exists(path):
cmd = 'mkdir -p ' + path
os.system(cmd) # 正常的工作目录
if os.path.abspath('.') != path:
print('系统检查到不是在规定的目录下执行,正在拷贝到正确目录下!')
print(sys.argv[0])
cmd = 'rm -rf ' + path + (sys.argv[0]).split('/')[-1]
print(cmd)
os.system(cmd)
cmd='cp '+os.path.abspath('.')+'/'+sys.argv[0]+' '+path+sys.argv[0]
print(cmd)
os.system(cmd) # 执行测试命令
cmd = 'redis-cli -h ' + REDIS_IP + ' -p ' + REDIS_PORT + ' set a 1' while True:
result = t.getoutput(cmd)
if 'Connection refused' in result:
print('redis连接被拒绝,服务没有开启!正在开启...' + getCurrentTime())
cmd2 = 'service redis start'
os.system(cmd2)
time.sleep(1)
elif 'LOADING Redis is loading the dataset in memory' in result:
print('redis正在加载中...' + getCurrentTime())
time.sleep(1)
else:
print('redis正常!' + getCurrentTime())
break # 重新启动Tomcat
RestartTomcat() # 替换 /usr/local/tomcat7/bin/startup.sh
# 检查cron中是否存在本程序的进程
# 将文件读取到内存中
foundDownload = False
with open('/etc/rc.local', "r", encoding="utf-8") as f:
lines = f.readlines() # 删除某行
with open("/etc/rc.local", "w", encoding="utf-8") as f_w:
for line in lines:
if "/usr/local/tomcat7/bin/startup.sh" in line:
continue
if "python3 /usr/local/software/CloudPlatformUtil/AutoDeployment/StartTomcat.py" in line:
continue
f_w.write(line) # 增加某行
with open('/etc/rc.local', 'a') as f:
f.write('python3 /usr/local/software/CloudPlatformUtil/AutoDeployment/StartTomcat.py' + '\n') print('恭喜,所有动作成功完成!')
解决Tomcat因Redis加载慢而启动失败的问题的更多相关文章
- hibernate+spring mvc,解决hibernate对象懒加载,json序列化失败
在使用spring MVC时,@ResponseBody 注解的方法返回一个有懒加载对象的时候出现了异常,以登录为例: @RequestMapping("login") @Resp ...
- Fullcalendar改版后发布到IIS或者tomcat里面前端加载数据不显示的问题
问题如题:Fullcalendar改版后发布到IIS或者tomcat里面前端加载数据不显示的问题 解决办法:通过火狐浏览器工具发现是时间格式不对的原因,需要将时间格式修改为:yyyy-MM--DD ...
- iOS 解决LaunchScreen中图片加载黑屏问题
iOS 解决LaunchScreen中图片加载黑屏问题 原文: http://blog.csdn.net/chengkaizone/article/details/50478045 iOS 解决Lau ...
- Tomcat的class加载的优先顺序
Tomcat的class加载的优先顺序一览 1.最先是$JAVA_HOME/jre/lib/ext/下的jar文件. 2.环境变量CLASSPATH中的jar和class文件. 3.$CATALINA ...
- 解决 Visual Studio 符号加载不完全问题
解决 Visual Studio 符号加载不完全问题 工具 - 选项 - 搜索 "符号" - 选上服务器 | 加载所有符号, 之后符号就会显示完全
- 解决页面初始化vue加载代码问题
<style type="text/css"> /* 解决页面初始化vue加载代码问题 */ [v-cloak] { display: none; } </sty ...
- Hive中将文件加载到数据库表失败解决办法
Hive中将文件加载到数据库表失败解决办法(hive创建表失败) 遇到的问题: FAILED: Execution Error, return code 1 from org.apache.hadoo ...
- Tomcat 7 自动加载类及检测文件变动原理
在一般的 web 应用开发里通常会使用开发工具(如 Eclipse.IntelJ )集成 tomcat ,这样可以将 web 工程项目直接发布到 tomcat 中,然后一键启动.经常遇到的一种情况是直 ...
- Tomcat是如何加载Spring和SpringMVC及Servlet相关知识
概述 大家是否清楚,Tomcat是如何加载Spring和SpringMVC,今天我们就弄清下这个过程(记录最关键的东西) 其中会涉及到大大小小的知识,包括加载时候的设计模式,Servlet知识等,看了 ...
随机推荐
- CentOS7搭建 Hadoop + HBase + Zookeeper集群
摘要: 本文主要介绍搭建Hadoop.HBase.Zookeeper集群环境的搭建 一.基础环境准备 1.下载安装包(均使用当前最新的稳定版本,截止至2017年05月24日) 1)jdk-8u131 ...
- mybatis中parameterType可以写的别名
mybatis中parameterType可以写的别名 https://blog.csdn.net/sdzhangshulong/article/details/51749807 _byte byte ...
- ASP.NET基础学习未整理随笔
利用新建网站添加的项目可以改完代码直接刷新就行 提交到服务程序的表单一定要具有name.<input type="hidden"value="true"n ...
- json 拼二维json数组
js声明数组 以及向数组中添加as移除json数据 JavaScript声明JSON数组的方法: //部分条件,在数据渲上数据要求是数组格式而非json数组格式,取arrayJson.dataList ...
- 多条select语句的合并
select (') , (') , (select max(ssq) from DW_MARK_FXJS.F_GS_YCXJJDCSY@new_zgxt ) from dual 这样就不用傻傻的执行 ...
- RabbitMQ的使用总结
RabbitMQ介绍 说明: Consumer (消费者):使用队列 Queue 从 Exchange 中获取消息的应用. Exchange (交换机):负责接收生产者的消息并把它转到到合适的队列. ...
- Linux下Tomcat重启脚本
我们重启Tomcat服务的时候,Tomcat自带的shutdown.sh脚本有时并不能真正杀死进程,经常需要我们用“kill -9 pid”的方式来杀死进程. 下面的脚本可以简化我们的操作,执行可杀死 ...
- mysql 可视化界面操作指令
1.让自增长从新开始 ALTER TABLE users auto_increment =1;//让表中的自增长从新从0开始 2.条件查询 SELECT name from users WHERE ...
- Linux while 和 read 的用法
Reference: [ linux man doc ] [ CSDN roler_ ] [ Reads from the file descriptor] read 命令说明 SYNTAX : re ...
- Codeforces Round #478 (Div. 2)
题目链接:http://codeforces.com/contest/975 A. Aramic script time limit per test:1 second memory limit pe ...