将一下3个脚本放到一个目录里,做成一个 python 的包即可使用
脚本介绍
操作服务脚本
#!/usr/bin/env python # _*_coding:utf-8_*_ # Author: "Edward.Liu" # Author-Email: lonnyliu@126.compile """ process hanlde files incloud 1.process status use request get Url returncode 2.process Stop use psutil kill process 3.process start use subprocess run shell start process 4.process log use process logs 5.process restart """ # Improt Libary import psutil from subprocess import PIPE, Popen, STDOUT import os import sys import requests import datetime # Set vars process_name = "/software/apache-tomcat-jenkins" url = "http://172.31.1.230:8080" def process_id(): # use process name get process pid process_base_str = "-Dcatalina.base=%s" % process_name pid = {} for proc in psutil.process_iter(): try: pinfo = proc.as_dict(attrs=['pid', 'name', 'cmdline']) except psutil.NoSuchProcess: pass else: if process_base_str in pinfo.get('cmdline'): pid[process_name] = pinfo.get('pid') return pid def process_judgment(): # 判断 URL 的状态码 messages = "" try: jenkins_r = requests.get(url, timeout=None) return jenkins_r.status_code except requests.ConnectTimeout: messges = "Timeout" return messges except requests.ConnectionError: messages = "connection" return messages except requests.HTTPError: messages = "httperror" return messages else: return messages def process_status(): # judgment process status if not process_id().get(process_name) is None: if process_judgment() == 200: print "