根据关键字找进程id


#!/usr/bin/python
import subprocess
import sys
import logging
import os

gameproc = "jd_5.py"

def getPid(process):
cmd = "ps aux| grep '%s'|grep -v grep " % process
logging.info(cmd)
out = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
infos = out.stdout.read().splitlines()
pidlist = []
if len(infos) >= 1:
for i in infos:
pid = i.split()[1]
if pid not in pidlist:
pidlist.append(pid)
return [x.decode() for x in pidlist]
else:
return -1

pid = getPid(gameproc)
print(pid)

posted @ 2017-11-28 17:31  wangheng1409  阅读(375)  评论(0编辑  收藏  举报