-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto-restart-tomcat.py
More file actions
51 lines (44 loc) · 1.67 KB
/
Copy pathauto-restart-tomcat.py
File metadata and controls
51 lines (44 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Tomcat 服务自动重启监控脚本
功能描述:
本脚本用于监控 Tomcat 服务的运行状态,当检测到 Tomcat 服务停止时自动重启服务。
通过持续的服务状态检查和自动恢复机制,确保 Tomcat 服务的高可用性。
技术实现:
- 使用 subprocess 模块执行系统命令
- 通过 ps aux + grep 组合命令检测进程
- 使用 catalina.sh 脚本启动 Tomcat 服务
- 基于时间间隔的循环监控机制
"""
import time, subprocess
def check_tomcat() -> bool:
try:
# grep 要把grep和python脚本本身的进程排除出去
result = subprocess.run("ps aux | grep tomcat | grep -v grep | grep -v python", shell=True, capture_output=True, check=True, text=True)
if result.stdout:
print("Tomcat is running.")
return True
else:
print("Tomcat is not running")
return False
except Exception as e:
print(f"An error occurred while checking Tomcat status: {e}.")
return False
def start_tomcat():
try:
tomcat_start_command="/opt/tomcat/bin/catalina.sh start"
result = subprocess.run(tomcat_start_command, shell=True, text=True, capture_output=True, check=True)
if result.returncode == 0:
print("Tomcat started successfully.")
else:
print("Failed to start tomcat.")
except Exception as e:
print(f"{e}")
def monitor_tomcat(interval):
while True:
if not check_tomcat():
start_tomcat()
else:
print("No action needed, tomcat is running.")
time.sleep(interval)
if __name__ == '__main__':
monitor_tomcat(60)