Limit Top Command to Only Display Top X Processes on Command Line

Limit top command to only display top X processes on command line

> top

then, press n to set maximum tasks displayed.

When operating top, one of the most important key is help (h or ?) to see the available options (n is given in help).

UPDATE (after the the comment):

PERSONAL Configuration File might help for the batch mode. Run top then set the maximum tasks displayed with n and use the W interactive command to create or update the configuration file. top will be ran according to the configuration file next time.

Getting the first 5 lines from PID and COMMAND using top

With your shown samples, please try following code. Since you need top 5 PIDs details so printing first 5 lines wouldn't work here. So skipping first 6 lines in output of top(which are about system details).

top -b | awk 'FNR>=7 && FNR<=12{print $1};FNR==12{exit}'

Explanation: Simple explanation of above code would be, passing top command's output to awk as an standard input. Then in awk program checking condition if line number is from 7th to 12th then print it and on 12th line exit out of program.

Where definition of top -b option is as follows from man top:

b :Batch-mode operation Starts top in Batch mode, which could be
useful for sending output from top to other programs or to a file. In
this mode, top will not accept input and runs until the iterations
limit you've set with the `-n' command-line option or until killed.

How can I find a specific process with top in a Mac terminal

Use this instead: ps -ax | grep -i skype

How to capture the output of a top command in a file in linux?

for me top -b > test.txt will store all output from top ok even if i break it with ctrl-c. I suggest you dump first, and then grep the resulting file.

Find and kill a process in one line using bash and regex

In bash, you should be able to do:

kill $(ps aux | grep '[p]ython csp_build.py' | awk '{print $2}')

Details on its workings are as follows:

  • The ps gives you the list of all the processes.
  • The grep filters that based on your search string, [p] is a trick to stop you picking up the actual grep process itself.
  • The awk just gives you the second field of each line, which is the PID.
  • The $(x) construct means to execute x then take its output and put it on the command line. The output of that ps pipeline inside that construct above is the list of process IDs so you end up with a command like kill 1234 1122 7654.

Here's a transcript showing it in action:

pax> sleep 3600 &
[1] 2225
pax> sleep 3600 &
[2] 2226
pax> sleep 3600 &
[3] 2227
pax> sleep 3600 &
[4] 2228
pax> sleep 3600 &
[5] 2229
pax> kill $(ps aux | grep '[s]leep' | awk '{print $2}')
[5]+ Terminated sleep 3600
[1] Terminated sleep 3600
[2] Terminated sleep 3600
[3]- Terminated sleep 3600
[4]+ Terminated sleep 3600

and you can see it terminating all the sleepers.


Explaining the grep '[p]ython csp_build.py' bit in a bit more detail:

When you do sleep 3600 & followed by ps -ef | grep sleep, you tend to get two processes with sleep in it, the sleep 3600 and the grep sleep (because they both have sleep in them, that's not rocket science).

However, ps -ef | grep '[s]leep' won't create a process with sleep in it, it instead creates grep '[s]leep' and here's the tricky bit: the grep doesn't find it because it's looking for the regular expression "any character from the character class [s] (which is s) followed by leep.

In other words, it's looking for sleep but the grep process is grep '[s]leep' which doesn't have sleep in it.

When I was shown this (by someone here on SO), I immediately started using it because

  • it's one less process than adding | grep -v grep; and
  • it's elegant and sneaky, a rare combination :-)


Related Topics



Leave a reply



Submit