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

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to display only the first 5 running process using top in shell scripting?
# 1  
Old 11-21-2016
Linux How to display only the first 5 running process using top in shell scripting?

Code:
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?
# 2  
Old 11-21-2016
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 :
Code:
top -n1 | grep -A5 "PID"

Use -d1 for e.g. FreeBSD...
This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-21-2016
Catch the first 5 lines that start with a number.
Code:
top -b -n 1 | awk '$1+0 && ++n<=5'

# 4  
Old 11-21-2016
Hey Rudic,
its working as expected,but why we need to give n1?
without giving n1 is also giving the same output
# 5  
Old 11-21-2016
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...
# 6  
Old 11-21-2016
Thats cool
Thanks buddy
# 7  
Old 11-21-2016
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):

Code:
# 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
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display current directory for a running process for script

I'm trying to create a ksh script to do the following: 1) Ask the user what process they want to search for. 2) Have the script read the input as a variable and use it to search for the process. 3) Display the current time & date, the working directory of the process, and finally display the... (6 Replies)
Discussion started by: seekryts15
6 Replies

2. Shell Programming and Scripting

Need help in shell Scripting to display a output from a command

Please find my below requirement and see if you can help me on this. I am looking for a shell script which can provide me the below output. Manuall steps which i am doing now 1) First I source the File $ . ./WC_env.sh 2) Execute the command $ /app/oracle/product/mos/bin/mosotl -url... (2 Replies)
Discussion started by: sudheshpn@gmail
2 Replies

3. Shell Programming and Scripting

Display the First and Last name from a file using shell scripting

I am new to shell scripting and doing a similar thing @ work Stan:Smith:Detroit:MI Jim:Jones:Farmington Hills:MI Jack:Frost:Denver:CO Sue:Apple:New York:NY Cindy:Thompson:Battle Creek:MI John:Smith:Denver:CO George:Jones:New York:NY Need to create a shell script This script will display... (1 Reply)
Discussion started by: jakemathew
1 Replies

4. Shell Programming and Scripting

How to calculate months and display in shell scripting

I just want to know, how do we calculate the months in shell scripting. If i give the input as 20-01-2011, the output should be 20-02-2011, 20-03-2011 or 20-04-2011........ How do i get this ? Cheers. (6 Replies)
Discussion started by: sachin24
6 Replies

5. Shell Programming and Scripting

Shell script to display top 5 process (per cpu consumption)

Hello, I have to create shell script being myself real novice in scripting... I was looking on the Internet for some script examples, but I don't have the basic understanding of the shell. I was trying to do something with “ps” command without success. For exemple total number of... (2 Replies)
Discussion started by: mr_awd
2 Replies

6. UNIX for Dummies Questions & Answers

perl scripting for checking if a process is running

Hi All, I am new to perl and have been trying to write a short script to check a process.Though i havent reached to the stage where i can match the output. I am trying to pass a variable x with value /opt/RGw/csbp-base/CSBP_BAT.01.00.05/csbp_BAT.01.00.05.jar and then pass another variable... (2 Replies)
Discussion started by: pistachio
2 Replies

7. UNIX for Dummies Questions & Answers

Shell scripting adding text to top of file

Hi this is quite simple i am sure but without using awk or sed i need to add text to the top of a file this is what i have got so far #!bin/bash echo "Add text to top of file" read line echo $line >> file1 This adds the text to the bottom of the file can some1 please help cheers (7 Replies)
Discussion started by: meadhere
7 Replies

8. UNIX for Dummies Questions & Answers

top ten utilities in shell scripting?

Let's get some feedback about the top ten ute's you guys use in writing your scripts - I mean yeah it depends on the job and what you're trying to accomplish, but there ARE those commands (sed, grep, awk, cut, etc.) that most will use time and again... ...so, what do you use? (3 Replies)
Discussion started by: diego
3 Replies

9. UNIX for Advanced & Expert Users

Top running process

Hi, I have an oracle process running on top for a week now, but I couldnt see the same process with in oracle. how do I know what this process is? -GK P.S: when I say i didn't see within oracle, what I mean is I didn't see this process through oracle utility which shows all the oracle process (1 Reply)
Discussion started by: caprikar
1 Replies

10. UNIX for Dummies Questions & Answers

How Can I Have Top Display The Top 20 Processes??

how can i do that in a script withough havin the script halt at the section where the top command is located. am writign a script that will send me the out put of unx commands if the load average of a machine goes beyond the recommended number. top -n 20 i want to save this output to a file... (1 Reply)
Discussion started by: TRUEST
1 Replies
Login or Register to Ask a Question