26

I'm trying to make a small command that will find the processes that use the most CPU power.

Firstly, I use ps aux > file.txt and then cut -c 16-20 file.txt | sort -n | tail -5.

The result I get is this:

1.0 
2.7
8.
14.5
14.5 

So my question is how can I have both the %CPU usage and the other fields outputted together?

4
  • Why not use top instead? – Joseph R. Aug 28 '13 at 22:38
  • @JosephR. I just wanna use the commands that I just learned for text processing on this small 'script'. – Spyros Chiotakis Aug 28 '13 at 22:44
  • 1
    Well, if you're doing it as a sort of exercise, then you might want to look at awk. Afterwards, when you're ready to move things "to production", you should use top in batch mode if it's suitable for your requirements. – Joseph R. Aug 28 '13 at 22:46
  • 2
    You can also eliminate the intermediate file.txt by doing ps aux|cut -c ... – Joseph R. Aug 28 '13 at 22:47
40

The correct answer is:

ps --sort=-pcpu

For top 5:

ps --sort=-pcpu | head -n 6

So you can specify columns without interfering with sorting.

Ex:

ps -Ao user,uid,comm,pid,pcpu,tty --sort=-pcpu | head -n 6

Note of 'ckujau': --sort is supported by ps from procps, other implementations may not have this option.

3
  • This is indeed the correct answer. – Felipe Alvarez Mar 6 '15 at 5:29
  • 2
    Please note that --sort is only supported by ps from procps, other implementations may not have this option. – ckujau May 16 '15 at 23:29
  • Thank you 'ckujau' for noting this. I edited the answer. – Facundo Victor May 18 '15 at 2:13
14

Sort on the appropriate field (by default defined as whitespace to non-whitespace transition), in my case it is the 3rd one:

ps aux | sort -n -k 3
3
  • This isn't as good as @Facundo's answer, because if you change the visible columns, you'll have to also change the sort key. – Jonathan Hartley Dec 15 '15 at 19:31
  • 2
    @JonathanHartley yes, but only if your ps has the --sort option. – peterph Dec 15 '15 at 23:06
  • Fair enough. And a minimum comment length. – Jonathan Hartley Dec 16 '15 at 15:23
5

One annoyance with the accepted solution is that the headers for the different columns will get included in the output. Another issue being that when you use the switch -x to ps it will provide the full command lines for some programs, which can run on and become a nuisance in the output.

For example

  1. Annoying command-lines

    saml 18174 7.3 3.2 1174896 259756 ? Sl Aug27 132:38 /opt/google/chrome/chrome --type=renderer --lang=en-US --force-fieldtrials=AsyncDns/AsyncDnsB/ForceCompositingMode/disable/InfiniteCache/No/OmniboxHQPReplaceHUPProhibitTrumpingInlineableResult/Standard/OmniboxSearchSuggestTrialStarted2013Q1/7/OneClickSignIn/Standard/Prerender/PrerenderControl/SendFeedbackLinkLocation/default/Test0PercentDefault/group_01/UMA-Dynamic-Binary-Uniformity-Trial/default/UMA-Session-Randomized-Uniformity-Trial-5-Percent/group_17/UMA-Uniformity-Trial-1-Percent/group_20/UMA-Uniformity-Trial-10-Percent/group_07/UMA-Uniformity-Trial-20-Percent/default/UMA-Uniformity-Trial-5-Percent/group_04/UMA-Uniformity-Trial-50-Percent/group_01/ --enable-crash-reporter=4061BD10D0DC127169CE2132BC3FF8EC,Fedora release 14 (Laughlin) --extension-process --renderer-print-preview --disable-webgl --disable-pepper-3d --disable-gl-multisampling --disable-accelerated-compositing --disable-accelerated-2d-canvas --disable-accelerated-video-decode --channel=18086.4.1423385819

    root 1764 7.7 0.3 163692 28928 tty1 Ss+ Aug21 817:49 /usr/bin/Xorg :0 -nr -verbose -auth /var/run/gdm/auth-for-gdm-fADI0V/database -nolisten tcp vt1

    Chrome especially can be quite annoying when looking at ps output like this!

  2. Headers in output

    $ ps auf | sort -nk 3 | head -20
    ...
    root      1741  0.0  0.0   4140   400 tty5     Ss+  Aug21   0:00 /sbin/mingetty /dev/tty5
    root      1743  0.0  0.0   4140   400 tty6     Ss+  Aug21   0:00 /sbin/mingetty /dev/tty6
    saml     13780  0.0  0.0 115088  4216 pts/13   Ss   Aug27   0:00 bash
    saml     20432  0.0  0.0   6308   400 pts/9    S+   Aug27   0:00  \_ inotifywatch -e CREATE /etc
    saml      8206  0.0  0.0 115296  1108 pts/6    Ss+  Aug21   0:03 bash
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    saml     29352  0.1  0.5 661848 42960 pts/13   Sl+  17:59   0:18  \_ irb
    ...
    

    You can see the header mixed in at the 7th line in the output.

Alternatives

  1. Getting rid of the headers

    ps provides a switch for disabling the display of these column headers. It's pretty easy to use, simply add the --no-heading switch.

    $ ps aux --no-heading | sort -nk 3 | head -10
    68        1481  0.0  0.0  38592  2144 ?        Ssl  Aug21   0:28 hald
    68        1527  0.0  0.0  18016   672 ?        S    Aug21   0:01 /usr/libexec/hald-addon-acpi
    avahi     1333  0.0  0.0  27872  1172 ?        S    Aug21   0:01 avahi-daemon: running [grinchy.local]
    avahi     1334  0.0  0.0  27752    20 ?        S    Aug21   0:00 avahi-daemon: chroot helper
    dbus      1307  0.0  0.0  22716  1900 ?        Ss   Aug21   1:03 dbus-daemon --system
    gdm       1879  0.0  0.0 317220  2740 ?        Sl   Aug21   0:00 /usr/libexec/polkit-gnome-authentication-agent-1
    ntp       3060  0.0  0.0  32600  2152 ?        Ss   20:05   0:00 ntpd -u ntp:ntp -p /var/run/ntpd.pid -g
    root         1  0.0  0.0  19420  1004 ?        Ss   Aug21   0:01 /sbin/init
    root      1006  0.0  0.0      0     0 ?        S    Aug21   0:00 [kvm-irqfd-clean]
    root      1020  0.0  0.0      0     0 ?        S    Aug21   2:02 [kdmflush]
    

    Or like this:

    $ ps auf --no-heading | sort -nk 3 |head -10
    root      1735  0.0  0.0   4140   400 tty2     Ss+  Aug21   0:00 /sbin/mingetty /dev/tty2
    root      1737  0.0  0.0   4140   400 tty3     Ss+  Aug21   0:00 /sbin/mingetty /dev/tty3
    root      1739  0.0  0.0   4140   400 tty4     Ss+  Aug21   0:00 /sbin/mingetty /dev/tty4
    root      1741  0.0  0.0   4140   400 tty5     Ss+  Aug21   0:00 /sbin/mingetty /dev/tty5
    root      1743  0.0  0.0   4140   400 tty6     Ss+  Aug21   0:00 /sbin/mingetty /dev/tty6
    saml     13780  0.0  0.0 115088  4216 pts/13   Ss   Aug27   0:00 bash
    saml     20432  0.0  0.0   6308   400 pts/9    S+   Aug27   0:00  \_ inotifywatch -e CREATE /etc
    saml      2098  0.0  0.0 115508  3148 pts/9    Ss   Aug22   0:05 bash
    saml     27134  0.0  0.0 115496  6768 pts/18   Ss   Aug27   0:02 bash
    saml      3248  0.0  0.0 115076  1024 pts/0    Ss+  Aug21   0:00 bash
    
  2. Take control of the columns being displayed.

    This can seem like a pain, but if you're scripting the capture of ps output it isn't too bad. A command like this will mimic the output of ps aux, except swap out the longer form of the COMMAND column with a shorter version that just shows the executable's name.

    $ ps ax --no-headings -o user,pid,%cpu,%mem,vsz,sgi_rss,tname,stat,start_time,time,ucmd
    

    Putting it all together without the column headers it's much easier to deal with when scripting.

    List of highest to lowest % CPU processes:

    $ ps ax --no-headings -o user,pid,%cpu,%mem,vsz,sgi_rss,tname,stat,start_time,time,ucmd |sort -nrk 3|head -10
    saml     18086  114  6.4 1418852 515236 ?     Sl   Aug27 1-10:53:31 chrome
    saml     18536 14.7  4.1 1276240 328636 ?     Sl   Aug27 04:29:21 chrome
    saml     18558 12.2  3.4 1209712 278228 ?     Sl   Aug27 03:44:22 chrome
    saml     18543 10.9  3.8 1238132 308824 ?     Rl   Aug27 03:20:18 chrome
    root      1764  7.7  0.4 170220 35276 tty1    Rs+  Aug21 13:40:16 Xorg
    saml     18174  7.3  3.0 1167728 241728 ?     Sl   Aug27 02:14:25 chrome
    saml     18314  3.6  1.4 1049020 119308 ?     Sl   Aug27 01:06:19 chrome
    saml     18528  2.7  3.1 1178368 251212 ?     Sl   Aug27 00:50:55 chrome
    saml      2389  2.0  0.1 330632 8480 ?        Sl   Aug21 03:37:13 compiz
    saml     18208  1.7  1.3 1081132 108828 ?     Sl   Aug27 00:32:02 chrome
    

    List of lowest to highest % CPU processes:

    $ ps ax --no-headings -o user,pid,%cpu,%mem,vsz,sgi_rss,tname,stat,start_time,time,ucmd |sort -nk 3|tail -10
    saml     18208  1.7  1.3 1082156 108348 ?     Sl   Aug27 00:32:03 chrome
    saml      2389  2.0  0.1 330632 8492 ?        Sl   Aug21 03:37:15 compiz
    saml     18528  2.7  3.1 1181440 250856 ?     Sl   Aug27 00:50:58 chrome
    saml     18314  3.6  1.5 1050044 119828 ?     Sl   Aug27 01:06:25 chrome
    saml     18174  7.3  3.0 1167728 242068 ?     Sl   Aug27 02:14:32 chrome
    root      1764  7.7  0.3 163688 28716 tty1    Ss+  Aug21 13:40:26 Xorg
    saml     18543 10.9  3.8 1235060 305464 ?     Sl   Aug27 03:20:32 chrome
    saml     18558 12.2  3.5 1214832 283560 ?     Sl   Aug27 03:44:34 chrome
    saml     18536 14.7  3.9 1267024 314400 ?     Sl   Aug27 04:29:39 chrome
    saml     18086  114  6.4 1412992 514856 ?     Sl   Aug27 1-10:55:28 chrome
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.