[oracle@ycr python]$ more pro_get.py
import re
import subprocess
args = ['ps', 'aux']
ps = subprocess.Popen(args, stdout=subprocess.PIPE)
processes = ps.stdout.readlines()
header = re.split('\s+', processes.pop(0))[:-1]
header.remove('COMMAND')
PS = {}
for process in processes:
columns = re.split('\s+', process)
if columns[0]!='oracle':
continue
PS[int(columns[1])] = {}
for position, column in enumerate(columns[:9]):
PS[int(columns[1])][header[position].lower()] = column
PS[int(columns[1])]['command'] = ' '.join(columns[10:])
from pprint import pprint
pprint(PS)
输出结果如下:
[oracle@ycr python]$ python pro_get.py
{2564: {'%cpu': '0.0',
'%mem': '1.4',
'command': 'ora_pmon_ycr ',
'pid': '2564',
'rss': '27276',
'start': '04:09',
'stat': 'Ss',
'tty': '?',
'user': 'oracle',
'vsz': '1089560'},
2566: {'%cpu': '0.0',
'%mem': '1.0',
'command': 'ora_psp0_ycr ',
'pid': '2566',
'rss': '20112',
'start': '04:09',
'stat': 'Ss',
'tty': '?',
'user': 'oracle',
'vsz': '1089560'},
2568: {'%cpu': '1.9',
'%mem': '1.0',
'command': 'ora_vktm_ycr ',
'pid': '2568',
'rss': '19724',
'start': '04:09',
'stat': 'Ss',
'tty': '?',
'user': 'oracle',
'vsz': '1089560'},
2572: {'%cpu': '0.0',
'%mem': '1.7',
'command': 'ora_gen0_ycr ',
'pid': '2572',
'rss': '34032',
'start': '04:09',
'stat': 'Ss',
'tty': '?',
'user': 'oracle',
'vsz': '1091300'},
.................................