67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
#!/bin/python3
|
|
'''
|
|
Created on Mar 30, 2014
|
|
author: tpweis
|
|
file name: tomcat_stop.py
|
|
'''
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
sys.path.insert(0,'/sl/lib')
|
|
|
|
from sleds_utilities import *
|
|
|
|
def main():
|
|
"function main"
|
|
|
|
|
|
slsetvars()
|
|
|
|
scriptname = sltomcatbinget() + "shutdown.sh"
|
|
p1 = subprocess.Popen([scriptname], stdout=subprocess.PIPE)
|
|
result_data = p1.communicate()[0]
|
|
result_data=result_data.decode()
|
|
slscatlog('D',"tomcat_stop shutdown.sh returned: " + result_data)
|
|
|
|
# If the pid file doesn't exist or the pid value was wrong, shutdown.sh will fail
|
|
# This means we need to use pkill to stop tomcat.
|
|
if "Stop aborted" in result_data:
|
|
# First remove any pid file that exists
|
|
pid_name = slrunget() + "tomcat.pid"
|
|
if os.path.isfile(pid_name):
|
|
slscatlog('W', "Bad pid value, removing " +pid_name)
|
|
os.remove(pid_name)
|
|
|
|
# Now, kill tomcat using "pkill"
|
|
process_string = "/usr/local/tomcat/lib"
|
|
# This kills the process with tomcat in its description that is the oldest running.
|
|
p2 = subprocess.Popen(["pkill", "-U", "sequencelogic", "-f", process_string, "-o"], stdout=subprocess.PIPE)
|
|
# Did this kill it? Probably. Let's do a ps followed by a grep to be certain.
|
|
result_data = "FAIL to stop"
|
|
for n in range(10):
|
|
time.sleep(1)
|
|
p3 = subprocess.Popen(["ps", "-U", "sequencelogic", "a"], stdout=subprocess.PIPE)
|
|
p4 = subprocess.Popen(["grep", "/usr/local/tomcat/lib"], stdin=p3.stdout, stdout=subprocess.PIPE)
|
|
# now remove the grep from the result
|
|
p5 = subprocess.Popen(["grep", "-v", "grep"], stdin=p4.stdout, stdout=subprocess.PIPE)
|
|
data = p5.communicate()[0]
|
|
data=data.decode()
|
|
slscatlog('D',"tomcat_stop pkill returned: " + data)
|
|
# If /usr/local/tomcat/lib was not found in the grep, then tomcat is stopped.
|
|
if(len(data) > 0):
|
|
continue
|
|
else:
|
|
result_data = "Tomcat stopped"
|
|
break
|
|
|
|
print(result_data)
|
|
slscatlog('D',"tomcat_stop: " + result_data)
|
|
|
|
exit()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# execute only if run as a script
|
|
main()
|
|
|