Last active
November 4, 2019 07:01
-
-
Save seunggabi/b957c98e56bc3823985a259681ab054c to your computer and use it in GitHub Desktop.
check_restart_tomcat.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 5 * * * * python3 /home1/irteam/check_restart_tomcat.py | |
from urllib.request import urlopen | |
from urllib.error import HTTPError | |
import os | |
import time | |
URL = "" | |
TOMCAT_PATH = "/home/user/apps/tomcat/bin/" | |
SHUTDOWN = "shutdown.sh" | |
STARTUP = "startup.sh" | |
LOG_PATH = "/home/user/logs/python/" | |
LOG_FILENAME = "check_restart_tomcat.log" | |
def checkHttpStatus(url): | |
try: | |
res = urlopen(url) | |
return res.status | |
except HTTPError as e: | |
code = e.getcode() | |
return code | |
def restart(path, startup, shutdown): | |
log('restart!!!') | |
os.system(path + shutdown) | |
time.sleep(5) | |
# os.system("pkill java") | |
os.system(path + startup) | |
def log(message): | |
global LOG_PATH | |
global LOG_FILENAME | |
timestamp = time.strftime('[%Y-%m-%d(%H:%M:%S)] ') | |
timeline = str(timestamp) + message + '\n' | |
file = open(LOG_PATH + LOG_FILENAME, 'a') | |
file.write(timeline) | |
file.close() | |
status = checkHttpStatus(URL) | |
log("status: " + str(status)) | |
if status != 200: | |
restart(TOMCAT_PATH, STARTUP, SHUTDOWN) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OutOfMemory
pkill java