Reputation: 11
I am trying to develop this program which is used to start tomcat server version 7 using python. I am using ubuntu OS.
#!/usr/bin/python
import os
import subprocess
proc=raw_input("Enter the mode :")
os.environ["JAVA_HOME"] = '/usr/lib/jvm/java-7-openjdk-amd64/bin';
os.environ["JRE_HOME"]='/usr/lib/jvm/java-7-openjdk-amd64/jre';
os.environ["CATALINA_HOME"] = '/export/apps/tomcat7';
os.environ["PATH"] = '$JAVA_HOME/bin:$PATH';
if proc == "start":
subprocess.call(['/export/apps/tomcat7/bin/catalina.sh', 'start'])
elif proc == "stop":
subprocess.call(['/export/apps/tomcat7/bin/catalina.sh', 'stop'])
print "Tomcat stopped successfully"
elif proc == "restart":
subprocess.call(['/export/apps/tomcat7/bin/catalina.sh', 'stop'])
subprocess.call(['/export/apps/tomcat7/bin/catalina.sh', 'start'])
print "tomcat restarted successfully"
else:
print "error: give any mode"
print "Thank you"
But i keep getting this error.
/export/apps/tomcat7/bin/catalina.sh: 1: /export/apps/tomcat7/bin/catalina.sh: uname: not found /export/apps/tomcat7/bin/catalina.sh: 1: /export/apps/tomcat7/bin/catalina.sh: dirname: not found /export/apps/tomcat7/bin/catalina.sh: 1: /export/apps/tomcat7/bin/catalina.sh: tty: not found /export/apps/tomcat7/bin/catalina.sh: 379: /export/apps/tomcat7/bin/catalina.sh: touch: not found Anyone please help me to rectify this error..
Upvotes: 0
Views: 3723
Reputation: 469
The python script to start, stop, check status & restart tomcat. That works for Python 2.6
#!/usr/bin/env python
# This is an init script to start, stop, check status & restart tomcat.
import sys
import subprocess
import time
scriptName = 'tomact_init.py'
tomcatBinDir = '/apps/apache-tomcat-7.0.63/bin'
tomcatShutdownPeriod = 5
commandToCheckTomcatProcess = "ps -ef | grep -v grep | grep " + tomcatBinDir + " | wc -l"
commandToFindTomcatProcessId = 'ps -ef | grep -v grep | grep ' + tomcatBinDir + ' | awk \'{print $2}\''
def isProcessRunning():
pStatus = True
tProcess = subprocess.Popen(commandToCheckTomcatProcess, stdout=subprocess.PIPE, shell=True)
out, err = tProcess.communicate()
if int(out) < 1:
pStatus = False
return pStatus
def usage():
print "Usage: python " + scriptName + " start|stop|status|restart"
print "or"
print "Usage: <path>/" + scriptName + " start|stop|status|restart"
def start():
if isProcessRunning():
print "Tomcat process is already running"
else:
print "Starting the tomcat"
subprocess.Popen([tomcatBinDir + "/startup.sh"], stdout=subprocess.PIPE)
def stop():
if isProcessRunning():
print "Stopping the tomcat"
subprocess.Popen([tomcatBinDir + "/shutdown.sh"], stdout=subprocess.PIPE)
time.sleep(tomcatShutdownPeriod)
if isProcessRunning():
tPid = subprocess.Popen([commandToFindTomcatProcessId], stdout=subprocess.PIPE, shell=True)
out, err = tPid.communicate()
subprocess.Popen(["kill -9 " + out], stdout=subprocess.PIPE, shell=True)
print "Tomcat failed to shutdown, so killed with PID " + out
else:
print "Tomcat process is not running"
def status():
if isProcessRunning():
tPid = subprocess.Popen([commandToFindTomcatProcessId], stdout=subprocess.PIPE, shell=True)
out, err = tPid.communicate()
print "Tomcat process is running with PID " + out
else:
print "Tomcat process is not running"
if len(sys.argv) != 2:
print "Missing argument"
usage()
sys.exit(0)
else:
action = sys.argv[1]
if action == 'start':
start()
elif action == 'stop':
stop()
elif action == 'status':
status()
elif action == 'restart':
stop()
start()
else:
print "Invalid argument"
usage()
Upvotes: 1
Reputation: 11
Finally i got the solution to develop a python script used to start tomcat server automatically. Herewith i have submitted my code..
#!/usr/bin/python
import os
import subprocess
proc=raw_input("Enter the mode :")
os.environ["JAVA_HOME"] = '/usr/lib/jvm/java-7-openjdk-amd64'
os.environ["CATALINA_HOME"] = '/export/apps/tomcat7'
if proc == "start":
os.getcwd()
os.chdir("/export/apps/tomcat7/bin/")
os.getcwd()
subprocess.call('sh catalina.sh start',shell=True)
print "Tomcat started successfully"
elif proc == "stop":
os.getcwd()
os.chdir("/export/apps/tomcat7/bin/")
os.getcwd()
subprocess.call('sh catalina.sh stop',shell=True)
print "Tomcat stopped successfully"
elif proc == "restart":
os.getcwd()
os.chdir("/export/apps/tomcat7/bin/")
os.getcwd()
subprocess.call('sh catalina.sh stop',shell=True)
subprocess.call('sh catalina.sh start',shell=True)
print "tomcat restarted successfully"
else:
print "error: give any mode"
print "Thank you"
Upvotes: 1
Reputation: 3806
Python does not perform variable substitution as shell does, so you're passing a wrong PATH. You should try with
os.environ["PATH"] = '/usr/lib/jvm/java-7-openjdk-amd64/bin:%s' % os.environ["PATH"]
Upvotes: 0