Python脚本运维Tomcat / TomEE

查看运行状态

#!/usr/bin/python
import os, sys, time
ps_tomee_command = 'ps -aux | grep java | grep {0} | grep {1} | grep -v grep'
running_str = '\033[32mRunning\033[0m'
stopped_str = '\033[31mStopped\033[0m'

def status_tomee(tom):
    sys_command = ps_tomee_command.format(tomee_path, tom)
    channel = os.popen(sys_command, mode='r')
    line = channel.read()
    running = False
    process_id = ''
    cpu_percent = '0.0'
    mem_percent = '0.0'
    if line != '':
        process = line.split()
        process_id = process[1]
        cpu_percent = process[2]
        mem_percent = process[3]
        running = os.path.exists(rf'/proc/{process_id}')
    running_str = '\033[32mRunning\033[0m'
    stopped_str = '\033[31mStopped\033[0m'
    print(f'Tomee: {tom.ljust(11)}, Status: {running_str if running else stopped_str}, Process ID: {process_id.ljust(6)}, CPU(%): {cpu_percent.ljust(5)}, Mem(%): {mem_percent.ljust(5)}')

效果
在这里插入图片描述

完整脚本参考

#!/usr/bin/python
import os, sys, time
ps_tomee_command = 'ps -aux | grep java | grep {0} | grep {1} | grep -v grep'
tomee_path = r'/app/tomee'
log_file = r'/logs/catalina.out'
stop_sh = r'/bin/shutdown.sh'
start_sh = r'/bin/startup.sh'
srv_paths = []
command = ''
tomee = ''

def tip():
    print('''Usage: command [tomee]
    command:
    \tstart: start tomee
    \tstop: stop tomee
    \trestart: restart tomee
    \tstatus: view tomee status
    \tclear: clear tomee logs
    \ttail: tail tomee catalina.out
    tomee:
    \tempty means all tomee
    \texample:  xxxx xxxxxxxx ...''')

def check_tomee_running(tom):
    sys_command = ps_tomee_command.format(tomee_path, tom)
    channel = os.popen(sys_command, mode='r')
    line = channel.read()
    if line == '':
        return False
    process = line.split()
    proc_dir = f'/proc/{process[1]}'
    return os.path.exists(proc_dir)

def start_tomee(tom):
    print(f'Start Tomee {tom}')
    running = check_tomee_running(tom)
    if running:
        print(f'\033[33mWARN: Tomee {tom} is running, skip start\033[0m')
        return
    start_command = f'{tomee_path}/{tom}{start_sh}'
    os.system(f'sh {start_command}')

def start():
    if len(tomee) != 0:
        start_tomee(tomee)
    else:
        for srv in srv_paths:
            start_tomee(srv)

def stop_tomee(tom):
    print(f'Stop Tomee {tom}')
    running = check_tomee_running(tom)
    if not running:
        print(f'\033[33mWARN: Tomee {tom} is stopped, skip stop\033[0m')
        return
    stop_command = f'{tomee_path}/{tom}{stop_sh}'
    os.system(f'sh {stop_command}')
    sys_command = ps_tomee_command.format(tomee_path, tom)
    channel = os.popen(sys_command, mode='r')
    line = channel.read()
    process = line.split()
    process_id = process[1]
    os.system(f'kill -9 {process_id}')

def stop():
    if len(tomee) != 0:
        stop_tomee(tomee)
    else:
        for srv in srv_paths:
            stop_tomee(srv)

def status_tomee(tom):
    sys_command = ps_tomee_command.format(tomee_path, tom)
    channel = os.popen(sys_command, mode='r')
    line = channel.read()
    running = False
    process_id = ''
    cpu_percent = '0.0'
    mem_percent = '0.0'
    if line != '':
        process = line.split()
        process_id = process[1]
        cpu_percent = process[2]
        mem_percent = process[3]
        running = os.path.exists(rf'/proc/{process_id}')
    running_str = '\033[32mRunning\033[0m'
    stopped_str = '\033[31mStopped\033[0m'
    print(f'Tomee: {tom.ljust(11)}, Status: {running_str if running else stopped_str}, Process ID: {process_id.ljust(6)}, CPU(%): {cpu_percent.ljust(5)}, Mem(%): {mem_percent.ljust(5)}')

def status():
    if len(tomee) != 0:
        status_tomee(tomee)
    else:
        for srv in srv_paths:
            status_tomee(srv)

def clear_tomee(tom):
    print(f'Clear Tomee {tom} logs && temp && work')
    running = check_tomee_running(tom)
    if running:
        print(f'\033[33mWARN: Tomee {tom} is running, skip clear\033[0m')
        return
    os.system(f'rm -rf {tomee_path}/{tom}/temp/*')
    os.system(f'rm -rf {tomee_path}/{tom}/work/*')
    os.system(f'rm -rf {tomee_path}/{tom}/logs/*')
    lck_file = f'{tomee_path}/{tom}/data/hsqldb/hsqldb.lck'
    if os.path.exists(lck_file):
        os.system(f'rm -f {lck_file}')

def clear():
    if len(tomee) != 0:
        clear_tomee(tomee)
    else:
        for srv in srv_paths:
            clear_tomee(srv)

def tail():
    if len(tomee) == 0:
        print(f'\033[31mERROR: must provide tomee path\033[0m')
        exit(0)
    tail_file = f'{tomee_path}/{tomee}{log_file}'
    if not os.path.exists(tail_file):
        print(f'\033[31mWARN: {tail_file} not exists\033[0m')
        exit(0)
    print(os.system(f'tail -100f {tomee_path}/{tomee}{log_file}'))

def format_args():
    global command, tomee
    args = sys.argv
    if len(args) < 2:
        tip()
        exit(0)
    elif len(args) == 2:
        command = args[1]
    else:
        command = args[1]
        tomee = args[2]
    print(f'command: {command}, tomee: {tomee}')
    if len(tomee) != 0 and (not os.path.exists(tomee_path + '/' + tomee)):
        print(f'ERROR: {tomee_path + "/" + tomee} not found')
        exit(0)

def main():
    if 'start' == command:
        start()
    elif 'stop' == command:
        stop()
    elif 'restart' == command:
        stop()
        print('Waiting for 2 seconds')
        time.sleep(2)
        clear()
        start()
    elif 'status' == command:
        status()
    elif 'clear' == command:
        clear()
    elif 'tail' == command:
        tail()
    else:
        print(f'\033[31mERROR: invalid command: {command}\033[0m')

if __name__ == '__main__':
    for path in os.listdir(tomee_path):
        srv_paths.append(path)
    format_args()
    main()

建立软链接

将脚本复制到服务器中

ln -s ~/tomctl.py ~/bin/z
chmod +x ~/tomctl.py

命令列表

在这里插入图片描述

查看状态

启动 停止 重启 清理日志
z start [tomee]
z stop [tomee]
z restart [tomee]
z clear [tomee]
查看日志
z tail {tomee}

在这里插入图片描述

### Intellij IDEA 中 TomEETomcat 的区别 #### 用户界面和配置方式 在 IntelliJ IDEA 中,无论是部署到 Tomcat 或者 TomEE,都需要通过服务器配置页面完成设置。然而,由于两者支持的技术栈不同,在具体操作上会有所差异[^1]。 对于 Tomcat 而言,其主要提供了一个基础的Servlet容器功能;而 TomEE 则是在 Tomcat 基础之上增加了完整的Java EE特性集的支持,这意味着当选择 TomEE 作为应用服务器时,可以更方便地开发基于 EJB、JPA 等技术的企业级应用程序[^3]。 #### 部署流程对比 - **Tomcat**: 当创建一个新的 Artifacts 来打包项目并将其关联至 Tomcat Server Configuration 后即可启动服务。此时仅能享受最基本的Web层能力。 - **TomEE**: 类似于 Tomcat 的部署过程,但是因为内置了更多企业版组件(如 CDI Bean Manager),所以在构建过程中可能会涉及到额外依赖项的选择以及初始化参数设定等问题。 #### 功能扩展性 Apache TomEE 提供了一套更为全面的服务框架来满足复杂业务场景下的需求,比如事务管理器、消息队列连接工厂等高级特性的集成都更加便捷高效。因此如果目标是构建大型分布式系统,则推荐优先考虑采用 TomEE 平台来进行开发工作。 ```xml <!-- Maven POM 文件中的部分配置 --> <dependencies> <!-- 对应不同的应用场景可以选择引入相应的模块 --> <!-- 如果使用的是Tomcat --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-catalina</artifactId> <version>${tomcat.version}</version> <scope>provided</scope> </dependency> <!-- 若切换成TomEE则需替换为如下形式 --> <dependency> <groupId>org.apache.tomee</groupId> <artifactId>javaee-api</artifactId> <version>${tomee.version}</version> <scope>provided</scope> </dependency> </dependencies> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wxl302947229

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值