So, I am trying to request the “intensive” processes of processor 3 on a given machine, and I found this shell command: ps -eo pcpu,pid,user,args | sort -k 1 -r | head -3
I want to use this data inside a Python script, so I need to be able to output the output of the above command through a module subprocess. The following works, but just returns a huge string, since I do not limit it to the top of 3:
psResult = subprocess.check_output(['ps', '-eo', 'pcpu,user,args'])
I'm not quite sure how this works subprocess.check_output.. in a lean attempt, I tried:
subprocess.check_output(['ps', '-eo', 'pcpu,user,args', '|', 'sort', '-k', '1', '-r', '|', 'head', '-3'])
Which gives me an error: ps: illegal argument: |
How to use pipe symbol |inside Python or use some other way to sort without having to do an incredible amount of parsing on the huge string returned psResult = subprocess.check_output(['ps', '-eo', 'pcpu,user,args'])?
Thank! Regards, -kstruct