由于刚接触 Python,所以使用Python 书写一些小的脚本,进行备忘同时分享给大家

#!/usr/bin/env python
# _*_coding:utf-8_*_
# author: 'lonny'
# dateTime: '15/11/26'
# motto: 'Good memory as bad written' import subprocess
import time, sys
import platform
import psutil class Tomcat(object):
def __init__(self, Tomcat_Home, Tomcat_Log_Home, counnt=10):
self.Tomcat_Home = Tomcat_Home
self.Tomcat_Log_Home = Tomcat_Log_Home
self.counnt = counnt def system_info(self):
system_type = platform.system()
system_version = platform.linux_distribution()
system_bit = platform.architecture()
Boot_Start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(psutil.boot_time()))
Host_Memory = psutil.virtual_memory().total / 1024
Can_Use = psutil.virtual_memory().available / 1024
ipaddress = str(psutil.net_if_addrs()['eth0'][0])
processors = psutil.cpu_count(logical=False) print "||||||||||" + "\033[32m本机IP地址--->Eth0:" + "\033[31m %s\033[0m" % ipaddress.split(',')[1]
print "||||||||||" + "\033[32m系统信息:\033[0m" + "\033[31m%s %s-%s %s\033[0m" % (
system_type, system_version[0], system_version[1], system_bit[0])
print "||||||||||" + "\033[32m本机启动时间:\033[0m" + "\033[31m%s\033[0m" % Boot_Start
print "||||||||||" + "\033[32m本机总内存:\033[0m" + "\033[31m %sKB\033[0m" % Host_Memory + "\033[31m ---->%sMB \033[0m" % (
Host_Memory / 1024)
print "||||||||||" + "\033[32m本机可使用内存:\033[0m" + "\033[31m%sKB\033[0m" % Can_Use + "\033[31m ---->%sMB \033[0m" % (
Can_Use / 1024)
print "||||||||||" + "\033[32m本机cpu合数:\033[0m" + "\033[31m %s \033[0m" % processors def get_tomcat_pid(self):
commandcheckoutpid = "ps aux | grep java | grep -v grep | grep 'jenkins_tomcat' | awk '{print $2}'"
p = subprocess.Popen(commandcheckoutpid, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = p.communicate()
# print "\033[32m Jenkins Is Starting \033[0m" + "\033[31mPID:%s\033[0m" % stdout
return stdout def start_tomcat(self):
if len(self.get_tomcat_pid().strip()) != 0:
print "\033[32m Tomcat Is Started \033[0m"
else:
# Start Tomcat
command_start_tomcat = "%s/bin/startup.sh" % self.Tomcat_Home
p = subprocess.Popen(command_start_tomcat, stdin=subprocess.PIPE, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
stdout, stderr = p.communicate()
print stdout def stop_tomcat(self):
wait_sleep = 0
if len(self.get_tomcat_pid().strip()) == 0:
print "\033[32m Tomcat is Not Running \033[0m"
else:
command_stop_tomcat = "%s/bin/shutdown.sh" % self.Tomcat_Home
p = subprocess.Popen(command_stop_tomcat, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = p.communicate()
while (len(self.get_tomcat_pid().strip()) != 0):
print "\nwaiting for processes to exit"
wait_sleep += 1
time.sleep(1)
if wait_sleep == self.counnt:
command_Kill_tomcat = "kill -9 %s" % int(self.get_tomcat_pid().strip())
p = subprocess.Popen(command_Kill_tomcat, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = p.communicate()
print "\033[32m Stop Tomcat is sucessful \033[0m"
break def useage(self):
scrip_name = "jenkins.py"
print "\033[32m script use \033[0m" + "./" + scrip_name + "\033[31m start|stop|restart|status\033[0m"
print "or"
print "\033[32m Script use \033[0m" + "python <path>/" + scrip_name + "\033[31m start|stop|restart|status\033[0m" def tomcat_log(self):
command_tomcat_log = "tail -f %s/catalina.out " % self.Tomcat_Log_Home
p = subprocess.Popen(command_tomcat_log, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
returncode = p.poll()
try:
while returncode is None:
line = p.stdout.readline()
returncode = p.poll()
line = line.strip()
print line
print returncode
except KeyboardInterrupt:
print 'ctrl+d or z' if __name__ == '__main__':
Handle = Tomcat("/software/jenkins_tomcat", "/software/jenkins_tomcat/logs")
if len(sys.argv) < 2:
Handle.useage()
sys.exit()
elif sys.argv[1] == 'start':
Handle.start_tomcat()
elif sys.argv[1] == 'stop':
Handle.stop_tomcat()
elif sys.argv[1] == 'restart':
Handle.stop_tomcat()
time.sleep(5)
Handle.start_tomcat()
elif sys.argv[1] == 'info':
Handle.system_info()
elif sys.argv[1] == 'log':
Handle.tomcat_log()
elif sys.argv[1] == 'status':
if len(Handle.get_tomcat_pid().strip()) != 0:
print "\033[32m Tomcat Is Running is PID:\033[0m" + "\033[31m %s \033[0m" % Handle.get_tomcat_pid().strip()
else:
print "\033[32m Tomat Not Running \033[0m"
else:
Handle.useage()

Tomcat Script(python)的更多相关文章

  1. Python Tomcat Script(多实例)

    之前书写过 Tomcat 单实例的 Python 脚本,本次增加 Tomcat 多实例的操作脚本. 1:准备 安装所需 Python 插件 A方法: pip install argparse B方法: ...

  2. rc.local 注意事項,call python script, file position

    如果要在 rc.local 呼叫 python script python script 的位置需使用絕對路徑 其 python script 裡的有關 file 的位置也需使用 絕對路徑 如果要在 ...

  3. Excel vba call Python script on Mac

    How can I launch an external python process from Excel 365 VBA on OSX? It took me a while, but I fig ...

  4. Python中文乱码

    1,注意:请使用智慧型浏览器 "CHROME" 配合理解和运作本文中提到的程序. 2,提示:谷歌的CHROME浏览器是迄今为止最智慧的浏览器,没有之一,只有第一. 3,谷歌的CHR ...

  5. Python基础篇【第6篇】: Python模块subprocess

    subprocess Python中可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen*          --废弃 popen2.*         ...

  6. 如何在batch脚本中嵌入python代码

    老板叫我帮他测一个命令在windows下消耗的时间,因为没有装windows那个啥工具包,没有timeit那个命令,于是想自己写一个,原理很简单: REM timeit.bat echo %TIME% ...

  7. Python Twisted、Reactor

    catalogue . Twisted理论基础 . 异步编程模式与Reactor . Twisted网络编程 . reactor进程管理编程 . Twisted并发连接 1. Twisted理论基础 ...

  8. The Python web services developer: XML-RPC for Python

    原文地址:http://www.ibm.com/developerworks/webservices/library/ws-pyth10/index.html 摘要:概括地说,您可以将 XML-RPC ...

  9. Tomcat自动启动脚本

    Tomcat自动启动脚本#!/bin/bash # chkconfig: 2345 10 90 # description: Starts and Stops the Tomcat daemon. T ...

随机推荐

  1. RS232/485通信方式 保存和加载时数据的处理

    RS232/485通信方式 数据以RS232/485方式通信时,以0xA5作为开始码,以0xAE作为结束码.在开始码和结束码之间的0xA5, 0xAA, 0xAE数据需要进行转码. PC端发送数据时将 ...

  2. Spreading the Wealth

    题目 A Communist regime is trying to redistribute wealth in a village. They have have decided to sit e ...

  3. Github中添加SSH key

    1-创建密钥,在终端输入下面的命令 ssh-keygen -t rsa -b -C "你的邮箱" //双引号不能去 要求输入密码,建议回车使用空密码方便以后的每次连接,此时会生成一 ...

  4. 洛谷 P1692 【部落卫队】

    啊这道题其实暴力就行了,算是一道搜索入门题吧. 搜索变量就应该是当前到哪一位了,然后进行枚举,当前的一位加或者不加,然后知道搜完为止. 判断当前一位可不可以加的时候本来想用vector的,但是没调出来 ...

  5. Python-使用tkinter实现的Django服务进程管理工具

    import tkinter import subprocess import os import time import re import sys from tkinter import Labe ...

  6. 为什么Spring Security看不见登录失败或者注销的提示

    有很多人在利用Spring Security进行角色权限设计开发时,一般发现正常登录时没问题,但是注销.或者用户名时,直接就回到登录页面了,在登录页面上看不见任何提示信息,如“用户名/密码有误”或“注 ...

  7. Layui的分页模块在网站中的应用

    制作网站的时候,有时候我们常常会被一些要求复杂的分页给困住,最后要么就是写一个简单的分页,要么就做成瀑布流的形式. 有了Layui之后,我认为开发人员多了一个选择,那就是尝试用Layui内置的分页模块 ...

  8. 分享几个很实用的CSS技巧对前端技术很有帮助

    创建剪切动画 对于剪切动画,使用clip-path代替width/height,避免DOM重排导致性能过低. .animate { width: 200px; height: 200px; backg ...

  9. 冷知识:达夫设备(Duff's Device)效率真的很高吗?

    ID:技术让梦想更伟大 作者:李肖遥 wechat链接:https://mp.weixin.qq.com/s/b1jQDH22hk9lhdC9nDqI6w 相信大家写业务逻辑的时候,都是面向if.el ...

  10. 棋子游戏 51Nod - 1534 思维题

    题目描述 波雷卡普和瓦西里喜欢简单的逻辑游戏.今天他们玩了一个游戏,这个游戏在一个很大的棋盘上进行,他们每个人有一个棋子.他们轮流移动自己的棋子,波雷卡普先开始.每一步移动中,波雷卡普可以将他的棋子从 ...