查看运行状态
#!/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}