943,731 Members | Top Members by Rank

Ad:
Sep 11th, 2009
-1

Kill top 5 processes

Expand Post »
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
Similar Threads
Reputation Points: 27
Solved Threads: 1
Newbie Poster
kranny is offline Offline
9 posts
since May 2009
Sep 11th, 2009
0

Re: Kill top 5 processes

What are the "top processes"? Highest CPU time, most memory usage, most disk IO?
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 11th, 2009
0

Re: Kill top 5 processes

Click to Expand / Collapse  Quote originally posted by sknake ...
What are the "top processes"? Highest CPU time, most memory usage, most disk IO?
m sorry..i meant top cpu usage processes
Reputation Points: 27
Solved Threads: 1
Newbie Poster
kranny is offline Offline
9 posts
since May 2009
Sep 11th, 2009
0

Re: Kill top 5 processes

I guess something like this will work:
bash Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 12th, 2009
0

Re: Kill top 5 processes

oh thanks..what does the k switch of sort do exactly?
Last edited by kranny; Sep 12th, 2009 at 5:22 am.
Reputation Points: 27
Solved Threads: 1
Newbie Poster
kranny is offline Offline
9 posts
since May 2009
Sep 12th, 2009
0

Re: Kill top 5 processes

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!
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 19th, 2009
0

Re: Kill top 5 processes

Click to Expand / Collapse  Quote originally posted by sknake ...
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
Reputation Points: 27
Solved Threads: 1
Newbie Poster
kranny is offline Offline
9 posts
since May 2009
Sep 19th, 2009
0

Re: Kill top 5 processes

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
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 26th, 2009
0

Re: Kill top 5 processes

Click to Expand / Collapse  Quote originally posted by sknake ...
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}'
Reputation Points: 27
Solved Threads: 1
Newbie Poster
kranny is offline Offline
9 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: cat . causes strange characters
Next Thread in Shell Scripting Forum Timeline: Record too long.......





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC