0

I have used following command to get a list of top heavy processes:

 ps aux --sort=-%mem

and the output is like:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
prsingh  20785 24.9 26.5 3581008 2084044 ?     Sl   11:28   4:26 /home/prsingh/.config/Ethereum Wallet/binaries/Geth/unpacked/geth --fast --cache 1024
prsingh   3029  1.0  7.6 1428144 602328 ?      Sl   08:26   2:10 /opt/google/chrome/chrome --type=renderer --field-trial-handle=1 --primordial-pipe-token=FDB365719E2EC19E6F4536F485544FC7 --lang=en-GB --ena
prsingh  24866 22.4  5.7 1236812 454032 ?      Sl   11:42   0:44 /opt/google/chrome/chrome --type=renderer --field-trial-handle=1 --primordial-pipe-token=88641F011686F1602A4BBEF8F2A30D49 --lang=en-GB --ena
prsingh   3088  0.2  5.3 1287836 417820 ?      Sl   08:26   0:31 /opt/google/chrome/chrome --type=renderer --field-trial-handle=1 --primordial-pipe-token=FECA2AB7525721EB2EE0DE7DBA8DB391 --lang=en-GB --ena
prsingh   2847  6.2  3.6 1388544 287132 ?      SLl  08:26  12:24 /opt/google/chrome/chrome    
root      1272  3.1  3.3 811276 259752 tty7    Ssl+ 08:25   6:20 /usr/lib/xorg/Xorg -core :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
prsingh   3558  0.1  2.5 950144 198620 ?       Sl   10:23   0:09 /opt/google/chrome/chrome --type=renderer --field-trial-handle=1 --primordial-pipe-token=57B4A5FE3EB41E14CD7D964712114F3A --lang=en-GB --ena
prsingh   2585  2.4  2.3 1555188 182048 ?      Sl   08:25   4:59 compiz

I am trying to convert each row to a list using python. I am unable to find the correct regex for this.

I want my output to be like:

['prsingh', 20785, 24.9, 26.5, 3581008 , 2084044, '?' , 's1' , 11:28 , '/home/prsingh/.config/Ethereum Wallet/binaries/Geth/unpacked/geth --fast --cache 1024' ]

What is the best way I can do that? I can't separate the string by spaces or tabs as they are giving incorrect output.

Is there a regex that can help or any other alternative?

This question is not a duplicate of Splitting out the output of ps using Python, the difference in in regex part. It can be done by splitting the string but I was willing to implemet a regex. Also the accpeted anser of the question doen't satisfies the need if I use a command that has same output but the COMMAND part is not at last.

To be specific if I use ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head command,

Community
  • 1
  • 1
Prashant Prabhakar Singh
  • 1,120
  • 4
  • 15
  • 33
  • Rather than do this all yourself, install the [`psutil` package](https://github.com/giampaolo/psutil) and have that project do this for you. – Martijn Pieters May 18 '17 at 06:56
  • 1
    `processes_by_memory = sorted(psutil.process_iter(), key=lambda p: p.memory_percent(), reverse=True)` – Martijn Pieters May 18 '17 at 07:00
  • @MartijnPieters Thanks, I was willing to use core python for this, but psutil is a good option. Can I get all the details of processes like the ps command in psutil like `user, pid, %cpu, %mem, startTime, processStatus, cmd` ? – Prashant Prabhakar Singh May 18 '17 at 10:22
  • Yes, see the [documentation](http://pythonhosted.org/psutil/). You can get that and more. – Martijn Pieters May 18 '17 at 10:25

0 Answers0