在Linux上,命令ps aux输出每个stat的多列的进程列表.例如
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND ... postfix 22611 0.0 0.2 54136 2544 ? S 15:26 0:00 pickup -l -t fifo -u apache 22920 0.0 1.5 198340 16588 ? S 09:58 0:05 /usr/sbin/httpd
我希望能够在使用Python时读取它,然后拆分每一行,然后拆分每一列,以便它们可以用作值.
在大多数情况下,这不是问题:
ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0] processes = ps.split('\n')
我现在可以遍历进程以获取每一行并将其拆分为空格
sep = re.compile('[\s]+') for row in processes: print sep.split(row)
但是,问题是最后一列命令有时会有空格.在上面的例子中,这可以在命令中看到
pickup -l -t fifo -u
将被拆分为
['postfix', '22611', '0.0', '0.2', '54136', '2544', '?', 'S', '15:26', '0:00', 'pickup', '-l', '-t', 'fifo', '-u']
但我真的想要它:
['postfix', '22611', '0.0', '0.2', '54136', '2544', '?', 'S', '15:26', '0:00', 'pickup -l -t fifo -u']
所以我的问题是,如何拆分列,但是当涉及到命令列时,将整个字符串保留为一个列表元素而不是按空格拆分?
使用第二个参数split
指定要将字符串拆分为的最大字段数.我想你可以通过计算第一行中的字段数,即列标题来找到该数字.
ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0] processes = ps.split('\n') # this specifies the number of splits, so the splitted lines # will have (nfields+1) elements nfields = len(processes[0].split()) - 1 for row in processes[1:]: print row.split(None, nfields)
Check out the python.psutils package.
psutil.process_iter
returns a generator which you can use to iterate over all processes.
p.cmdline
is a list of each Process object's cmdline arguments, separated just the way you want.
You can create a dictionary of pids vs (pid,cmdline,path)
with just one line and then use it anyway you want.
pid_dict = dict([(p.pid, dict([('pid',p.pid), ('cmdline',p.cmdline), ('path',p.path)])) for p in psutil.process_iter()]))