7

I need to print the 10 processes that are using the most CPU. Also I need to print their ID, and the command they were started with.


What I've found is that the command ps -ax -u prints all the processes and their %CPU usage. The command ps -ax -u --sort pcpu prints all the processes sorted by the %CPU usage, from the least to the most, but I need to print only 10 processes from the most to the least. I have to use something like sort -r to make a reverse sorting, but the command ps -ax -u --sort -r pcpu produces an error.


So, how can I make a reverse sorting and print only 10 of the processes?

1 Answer 1

12

to print 10 processes, that use the most CPU

ps -aux --sort -pcpu | head

Sorting syntax is [+|-]key[,[+|-]key[,...]].
The "+" is optional since default direction is increasing numerical or lexicographic order. Identical to k. For example: ps jax --sort=uid,-ppid,+pid


head - will print the first/top 10 lines of file(s) or standard input (by default)

2
  • 1
    And, for example, if I need not 10, but 15 lines, I will need to write head -n 15 ?
    – gloomikon
    Oct 3, 2017 at 10:04
  • 1
    @НиколайЖурба, that's right Oct 3, 2017 at 10:04

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.