zoukankan      html  css  js  c++  java
  • 【解决】用python启动tomcat服务脚本失败问题

    【背景】

    在测试敏捷开发项目时,迭代版本较频繁,每次手动重启tomcat服务较为繁琐,于是尝试编写python脚本,实现更换新包,重启tomcat服务功能。

    【过程】

    脚本编写完了执行时,发现远程启动Linux系统下tomcat无法启动,报错如图:

    Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
    At least one of these environment variable is needed to run this program

    【解决】

    百度后发现是tomcat/bin/setclasspath.sh 中需要制定JAVA HOME和JRE HOME,于是添加如下:

    保存后,重新执行启动tomcat脚本,启动成功:

    【附录脚本】

     1 import paramiko
     2 import sys
     3 
     4 HOST = '192.168.1.137'
     5 PORT = 22
     6 USER = 'root'
     7 PASSWD = '123456'
     8 #web配置文件目录
     9 DATABASE_DIR = r'D:项目资料abc3.0	mpweb配置文件datasource.xml'
    10 RESOURCE_DIR = r'D:项目资料abc3.0	mpweb配置文件
    esources.properties'
    11 #web配置文件目标目录
    12 DATABASE_DEST_DIR = r'/usr/tomcat/apache-tomcat-8.5.30/webapps/web/WEB-INF/classes/datasource.xml'
    13 RESOURCE_DEST_DIR = r'/usr/tomcat/apache-tomcat-8.5.30/webapps/web/WEB-INF/classes/resources.properties'
    14 
    15 
    16 #创建SHHClient示例对象
    17 ssh = paramiko.SSHClient()
    18 
    19 #调用方法,表示没有存储远程机器的公钥,允许访问
    20 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    21 
    22 #连接远程服务器 地址,端口,用户名密码
    23 ssh.connect(HOST,PORT,USER,PASSWD)
    24 
    25 
    26 def remoteRun(cmd, printOutput=True):
    27     """执行远程操作命令"""
    28     stdin, stdout, stderr = ssh.exec_command(cmd)
    29     output = stdout.read().decode()
    30     errinfo = stderr.read().decode()
    31     if printOutput:
    32         print(output + errinfo)
    33     return output + errinfo
    34 
    35 
    36 def replaceFile(dir1f1, dir2f1, dir1f2, dir2f2):
    37     """替换配置文件"""
    38     sftp = ssh.open_sftp()
    39     print('开始替换database.xml...')
    40     sftp.put(dir1f1, dir2f1)
    41     print('开始替换resources.properties...')
    42     sftp.put(dir1f2, dir2f2)
    43     sftp.close()
    44     print('替换完毕!!!')
    45     
    46 
    47 print("检查tomcat是否有运行......")
    48 output = remoteRun("ps -ef|grep tomcat|grep -v grep")
    49 
    50 #如果存在,则杀死进程
    51 if ("/usr/tomcat/apache-tomcat-8.5.30/bin/bootstrap.jar") in output:
    52     print("tomcat服务运行中,停止服务......")
    53     parts = output.split(' ')
    54     parts = [part for part in parts if part]
    55     pid = parts[1]
    56     #杀死进程
    57     output = remoteRun(f'kill -9 {pid}')
    58 
    59     #再次检查是否有先前版本运行
    60     output = remoteRun("ps -ef|grep tomcat|grep -v grep")
    61     if ("/usr/tomcat/apache-tomcat-8.5.30/bin/bootstrap.jar") in output:
    62         print("服务未能停止运行!!!")
    63         #退出进程
    64         sys.exit(3)
    65     else:
    66         print("服务停止成功")
    67 else:
    68     print("没有")
    69 
    70 
    71 #替换配置文件
    72 #replaceFile(DATABASE_DIR, DATABASE_DEST_DIR, RESOURCE_DIR, RESOURCE_DEST_DIR)
    73 
    74 
    75 #printOutput=True 打印输出
    76 print("启动tomcat")
    77 remoteRun("cd /usr/tomcat/apache-tomcat-8.5.30/bin;sh startup.sh;sleep 5",printOutput=True)
    78 
    79 print("检查tomcat是否运行成功")
    80 output = remoteRun('ps -ef|grep tomcat|grep -v grep')
    81 
    82 #如果存在,运行成功
    83 if ("/usr/tomcat/apache-tomcat-8.5.30/bin/bootstrap.jar") in output:
    84     print("tomcat服务运行成功")
    85 else:
    86     print('服务没有运行成功!')
    87     sys.exit(3)
    View Code
  • 相关阅读:
    Laravel5.0学习--03 Artisan命令
    yar框架使用笔记
    MySQL外键之级联
    笛卡尔积
    PHP钩子机制
    CentOS安装PHP7+Nginx+MySQL
    Linux压缩命令
    Git命令汇总
    使用List把一个长字符串分解成若干个短字符串
    Adb connection Error:远程主机强迫关闭了一个现有的连接
  • 原文地址:https://www.cnblogs.com/aikachin/p/9705504.html
Copyright © 2011-2022 走看看