Top utility in bash for loop | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Top utility in bash for loop

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-02-2012
mail2vivek1 mail2vivek1 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 2 December 2012, 8:07 PM EST
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Top utility in bash for loop

Dear All,

is it possible to use top utility in for loop?

I am trying to get top swap space consuming processes in my for loop.
I got multiple way (using /proc FS) however have anybody used top command in for loop? Using /proc FS giving solutionbut itrs more complex. Please suggest if anybody has simpler solution
Sponsored Links
    #2  
Old 12-02-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 21 November 2014, 11:17 AM EST
Location: Galactic Empire
Posts: 3,389
Thanks: 235
Thanked 1,210 Times in 1,136 Posts
You can do the following to achieve this:-
  • Run top command
top
  • Press O and then p to select sort by SWAP usage.
  • Press W to save this settings it to .toprc file
  • Now you can put below command in loop to get the list of processes sorted by SWAP usage:-
top -n 1

I hope this helps.
The Following User Says Thank You to Yoda For This Useful Post:
mail2vivek1 (12-02-2012)
Sponsored Links
    #3  
Old 12-02-2012
mail2vivek1 mail2vivek1 is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 2 December 2012, 8:07 PM EST
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks for reply Bipin!!!

However this method works only on terminal... any trick to use it in bash script/loop...

I meant, how the script will understand option O and P in for loop/shell script
    #4  
Old 12-02-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 21 November 2014, 11:17 AM EST
Location: Galactic Empire
Posts: 3,389
Thanks: 235
Thanked 1,210 Times in 1,136 Posts
Once you have saved the settings to .toprc using W option, then you don't have to use option O & p anymore. Your top command will always display processes sorted by SWAP usage. Please give it a try and see if you are getting the desired result.
Sponsored Links
    #5  
Old 12-03-2012
hombreopaco's Avatar
hombreopaco hombreopaco is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 26 August 2014, 12:56 PM EDT
Posts: 20
Thanks: 0
Thanked 4 Times in 4 Posts
Hi, you can use something like:

Code:
top -o size -n 1


Last edited by Franklin52; 12-03-2012 at 07:21 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using variables created sequentially in a loop while still inside of the loop [bash] DeCoTwc Shell Programming and Scripting 2 06-23-2009 05:59 PM
top command line utility antoniomorandi Programming 6 11-20-2007 08:38 PM
Unload utility with loop method cedrichiu Shell Programming and Scripting 1 07-02-2007 09:55 AM



All times are GMT -4. The time now is 12:37 AM.