#!/usr/bin/env python
# _*_coding:utf-8_*_
# Author: "Edward.Liu"
# Author-Email: lonnyliu@126.com
import psutil
import argparse
import sys
import subprocess
# 获取 Memcached 程序 pid~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def process_id():
pid = {} # 存放获取到的进程 ID 服务端口
for proc in psutil.process_iter(): # 迭代正在运行的运行的进程,进行排序
try:
pinfo = proc.as_dict(attrs=['pid', 'name', 'cmdline']) # 获取返回字符串的指定字段
except psutil.NoSuchProcess:
pass
else:
if pinfo.get('name') == 'memcached': # 通过字典获取指定的名称的进程
pid[pinfo.get('cmdline')[-1]] = pinfo.get('pid') # 将端口号与相应的进程 ID 关联存放到 pid{}
return pid
# 选择停止单个或者全部端口~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def select_stop(parameter):
if bool(process_id()): # 判断 pid 字典是否为空
try: # 判断输入的是否为数字
int(parameter.port)
if parameter.port in process_id().keys():
print "