Linux One Liners

ps -eo user,pcpu,pid,cmd | sort -r -k2 | head -6

What the heck does that do? It gives me this output:

greg 6.7 5901 /usr/lib/firefox-3.0/firefox
root 1.5 5441 /usr/bin/X :0 -br -audit 0 -auth /var/lib/gdm/:0.Xauth -nolisten tcp vt7
greg 0.5 7216 /usr/bin/liferea-bin
greg 0.1 7401 gnome-terminal
greg 0.1 5790 gnome-panel --sm-client-id default1

(results will vary)

This is a list of the top five most processor hungry applications running on my system right now.

ps stands for “processor stuff” and it returns stuff about the processor.

The e option tells ps to look at every process running on the system.

The o option allows the user to specify which of the many columns of data ps can produce.

The output of the ps command is piped, using the pipe symbol “|” to the sort command.

Sort takes a file or a stream of data (same thing, really) and … sorts it!

The r option sorts it in reverse. The k option tells sort which column to sort on (columns are defined by, say, the spaces between words and stuff) …. so here we are sorting on the second column.

The output of sort is then piped to the head command. The head command takes the top ten lines (the head) of a file and sends it back out again (‘head’ is a filter). But if you give it a number, it put out that number of lines.

Since ps puts out a row of column headers, to get the most processor intensive processes we ask head to give us 6 lines.

(Oh, it’s actually “processor snapshot”)


  1. #1 hank
    August 8, 2008

    Ah, this is pretty useful.

  2. #2 Leo Martins
    August 8, 2008

    ps also has a sort option: “–sort=-pcpu”
    The advantage over the piped “sort” is (besides less IO traffic) that you can decide how to break ties. And usually when I use the “sort” command with numeric values I put a “-n” to force numeric (instead of string) comparison.

  3. #3 Ben Zvan
    August 11, 2008

    This is pretty useful, but I’m wondering why you don’t just use ‘top’.

  4. #4 Greg Laden
    August 11, 2008

    Several reasons to not use top.

    1) too simple.

    2) it is an interactive program that does not by default stream the output. I assume you would want to use the output in some other command.

    3) i think there are some other cool things you can do with ps that you can stick in here. For instance, if you have a habitually badly behaved program, you can use a form of this one liner to isolate it, then send the out put to kill. Then you put this in a script that you run when you get annoyed at the program.

    4) But really, it is more of a demo program than anything else.

  5. #5 Ben Zvan
    August 11, 2008

    That makes sense, since ps is an invaluable command I couldn’t do without. I also tend to use pgrep and pkill pretty regularly.