Kill top 5 processes

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2009
Posts: 9
Reputation: kranny is an unknown quantity at this point 
Solved Threads: 1
kranny kranny is offline Offline
Newbie Poster

Kill top 5 processes

 
-1
  #1
Sep 11th, 2009
I want to write a script where in i can kill the top processes and run this script for every 5 minutes.How to extract parameters from top command
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,301
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 588
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Kill top 5 processes

 
0
  #2
Sep 11th, 2009
What are the "top processes"? Highest CPU time, most memory usage, most disk IO?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 9
Reputation: kranny is an unknown quantity at this point 
Solved Threads: 1
kranny kranny is offline Offline
Newbie Poster

Re: Kill top 5 processes

 
0
  #3
Sep 11th, 2009
Originally Posted by sknake View Post
What are the "top processes"? Highest CPU time, most memory usage, most disk IO?
m sorry..i meant top cpu usage processes
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,301
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 588
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Kill top 5 processes

 
0
  #4
Sep 11th, 2009
I guess something like this will work:
  1. sk@svn:~$ ps axu | grep -v ^USER | sort -k10 -r | head -n 5
  2. root 4045 0.0 0.2 6428 1392 ? Ss Aug11 0:20 /usr/sbin/nmbd -D
  3. root 4293 0.0 0.0 0 0 ? S< Aug11 0:19 [smbiod]
  4. sk 18362 0.0 0.3 8180 1720 ? S Aug29 0:09 sshd: sk@pts/0
  5. root 2246 0.0 0.0 0 0 ? S< Aug11 0:09 [kjournald]
  6. root 4085 0.0 0.1 2332 908 ? Ss Aug11 0:04 /usr/sbin/cron
  7. sk@svn:~$ ps axu | grep -v ^USER | sort -k10 -r | head -n 5 | awk '{ print $2 }'
  8. 4045
  9. 4293
  10. 18362
  11. 2246
  12. 4085
  13. sk@svn:~$ ps axu | grep -v ^USER | sort -k10 -r | head -n 5 | awk '{ print $2 }' | xargs echo kill -9
  14. kill -9 4045 4293 18362 2246 4085

FYI this seems like a very bad idea. You could render your system useless if you kill the wrong process. The codes I posted above also depends on how your ps renders the data (linux, fbsd, unix, etc) so be sure to test.

I'm sure Salem will be along with a more graceful solution
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 9
Reputation: kranny is an unknown quantity at this point 
Solved Threads: 1
kranny kranny is offline Offline
Newbie Poster

Re: Kill top 5 processes

 
0
  #5
Sep 12th, 2009
oh thanks..what does the k switch of sort do exactly?
Last edited by kranny; Sep 12th, 2009 at 5:22 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,301
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 588
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Kill top 5 processes

 
0
  #6
Sep 12th, 2009
Sorts on the column -- in this case k10 is the cpu-time column. see `man sort`.

Please mark this thread as solved if you have found an answer to your original question and good luck!
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 9
Reputation: kranny is an unknown quantity at this point 
Solved Threads: 1
kranny kranny is offline Offline
Newbie Poster

Re: Kill top 5 processes

 
0
  #7
Sep 19th, 2009
Originally Posted by sknake View Post
Sorts on the column -- in this case k10 is the cpu-time column. see `man sort`.

Please mark this thread as solved if you have found an answer to your original question and good luck!
what i meant to ask was wat does 10 denote here....is it that the CPU column started at 10th character position
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,301
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 588
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Kill top 5 processes

 
0
  #8
Sep 19th, 2009
It means the 10th column of data delimited by white spaces, not literally the 10th character from the left.

sk:~# ps axu | grep ^USER | sort -k10 -r
{1}USER       {2}PID {3}%CPU {4}%MEM    {5}VSZ   {6}RSS {7}TTY      {8}STAT {9}START   {10}TIME {11}COMMAND
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 9
Reputation: kranny is an unknown quantity at this point 
Solved Threads: 1
kranny kranny is offline Offline
Newbie Poster

Re: Kill top 5 processes

 
0
  #9
Sep 26th, 2009
Originally Posted by sknake View Post
It means the 10th column of data delimited by white spaces, not literally the 10th character from the left.

sk:~# ps axu | grep ^USER | sort -k10 -r
{1}USER       {2}PID {3}%CPU {4}%MEM    {5}VSZ   {6}RSS {7}TTY      {8}STAT {9}START   {10}TIME {11}COMMAND
So 3 should suffice my needs and also numeric sorting doesnt work correctly with sort unless a -g switch is used..So slightly modified cmd wud be
ps aux|sort -k 3 -g -r|head -n 6|awk '{awk print $2}'
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC