我想找出'ps'命令的输出是否包含进程'smtpd' 问题是各种busybox需要不同的ps命令! 有些需要'ps x',其他需要'ps w'而其他只需'ps'
我如何制作一个尝试所有'ps'可能性的通用算法?
示例:
linex=''
foo=os.popen('ps')
for x in foo.readlines():
if x.lower().find('smtpd') != -1:
// SOME SCRIPT STUFF on linex string...
return linex
linex=''
foo=os.popen('ps w')
for x in foo.readlines():
if x.lower().find('smtpd') != -1:
// SOME SCRIPT STUFF on linex string...
return linex
linex=''
foo=os.popen('ps x')
for x in foo.readlines():
if x.lower().find('smtpd') != -1:
// SOME SCRIPT STUFF on linex string...
return linex
答案 0 :(得分:1)
检查一下:
Process list on Linux via Python
/ proc是您找到所需内容的正确位置
import os
pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
for pid in pids:
try:
cmd = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read()
if cmd.find('smtpd') != -1:
print "PID: %s; Command: %s" % (pid, cmd)
# process has already terminated
except IOError:
continue
答案 1 :(得分:0)
def find_sys_cmds(needle,cmd,options):
for opt in options:
for line in os.popen("%s %s"%(cmd,opt)).readlines():
if needle in line.lower():
return line
print find_sys_cmds("smtpd","ps",["","x","w","aux",..."])
是你可以这样做的一种方式
如果您可能有多个匹配流程
def find_sys_cmds(needle,cmd,options):
for opt in options:
for line in os.popen("%s %s"%(cmd,opt)).readlines():
if needle in line.lower():
yield line
for line in find_sys_cmds("smtpd","ps",["","x","w","aux",..."]):
print line