需要自动启动tomcat

时间:2014-09-15 09:47:50

标签: python tomcat7

我正在尝试开发这个程序,用于使用python启动tomcat服务器版本7。我正在使用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"

但我一直收到这个错误。

/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 任何人都可以帮我纠正这个错误..

3 个答案:

答案 0 :(得分:1)

最后我得到了开发用于自动启动tomcat服务器的python脚本的解决方案。在此我提交了我的代码..

#!/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"

答案 1 :(得分:1)

启动,停止,检查状态&a​​mp;的python脚本重启tomcat。这适用于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()

答案 2 :(得分:0)

Python不像shell那样执行变量替换,因此您传递了错误的PATH。你应该试试

os.environ["PATH"] = '/usr/lib/jvm/java-7-openjdk-amd64/bin:%s' % os.environ["PATH"]