How to display only the first 5 running process using top in shell scripting?

topfunc()
{
top
}
topfunc

Here i used the top command inside a function,and i called the function.
when executing this bash file i get all the process which are using by the kernel
i just want to display only the first 5 running process.
is it possible?

This is not that easy, as it heavily depends on top 's configuration, and on the tools that are available on your system (which, by the way, you fail to mention in your request). My best result on my linux system :

top -n1 | grep -A5 "PID"

Use -d1 for e.g. FreeBSD...

1 Like

Catch the first 5 lines that start with a number.

top -b -n 1 | awk '$1+0 && ++n<=5'

Hey Rudic,
its working as expected,but why we need to give n1?
without giving n1 is also giving the same output

You need one single iteration of top . With -n1 , top leaves cleanly after that. Without it, it is killed because its output pipe has disappeared. Or, grep keeps waiting for more input...

Thats cool
Thanks buddy

Using top for this kind of task is generally a problem because top with its curses-designed interface was built for interactive use. You might want to consider using a different program.

For instance there is ps , which would also allow to sort for a more specific criteria than "whatever top places first". The process(es) with the most processor-time used? The processes with the most virtual memory allocated? The process using the most real memory? All this (and more) could be a realistic criteria for sorting (and identifying the top 5 of) the processes. With ps you do it this way:

First, look at the various arguments of the (SysV-style-) ps -option "-o" (see the man page): for this example i will use the size of the allocated virtual memory, which is "vsz". I make the virtual memory size the first column, then sort numerically on it. The output is from my system (Linux Fedora Core 22, Kernel 4.4.18):

# ps -Ao vsz,pid,cmd | sort -rn | head -n 5
2109716 1222 /usr/sbin/console-kit-daemon --no-daemon
1875216 3850 /usr/lib64/firefox/firefox
1243536 1482 nm-applet
1192252 1036 /usr/sbin/libvirtd
1007688 4249 /usr/lib64/firefox/plugin-container /usr/lib64/flash-plu[...]

Replace "vsz" by other arguments as your definition of "top" processes changes: "pcpu" for cpu consumption for instance, etc.

I hope this helps.

bakunin

1 Like

I suppose one should also ask which way you want to sort your processes. Is it by CPU use at that instant, total CPU use since process start, real memory, virtual memory, IO read, IO writes, file opens, network traffic, is the server using swap heavily etc.

There are many things to consider when a server, application or process is running slowly which may not be a simple "who is using the most CPU?"

Some examples are:

  • if your code is frequently spawning new processes to perform a menial task (e.g. using echo $var | cut -f1 -d " " in a loop)
  • if your code repeatedly opens a file to read one item in a loop rather than opening the file as input to the loop
  • if your code appends single lines to a file in a loop rather than having one output file descriptor for the loop
  • and many other pitfalls I'm sure.

Is there a particular problem you are having that you need to find out why it's running slow? Some more general information about your server, application and your perceived problem may help us to work on a better answer.

Kind regards,
Robin