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

Recommended Answers

All 8 Replies

What are the "top processes"? Highest CPU time, most memory usage, most disk IO?

What are the "top processes"? Highest CPU time, most memory usage, most disk IO?

m sorry..i meant top cpu usage processes

I guess something like this will work:

sk@svn:~$ ps axu | grep -v ^USER | sort -k10 -r | head -n 5
root      4045  0.0  0.2   6428  1392 ?        Ss   Aug11   0:20 /usr/sbin/nmbd -D
root      4293  0.0  0.0      0     0 ?        S<   Aug11   0:19 [smbiod]
sk       18362  0.0  0.3   8180  1720 ?        S    Aug29   0:09 sshd: sk@pts/0
root      2246  0.0  0.0      0     0 ?        S<   Aug11   0:09 [kjournald]
root      4085  0.0  0.1   2332   908 ?        Ss   Aug11   0:04 /usr/sbin/cron
sk@svn:~$ ps axu | grep -v ^USER | sort -k10 -r | head -n 5 | awk '{ print $2 }'
4045
4293
18362
2246
4085
sk@svn:~$ ps axu | grep -v ^USER | sort -k10 -r | head -n 5 | awk '{ print $2 }' | xargs echo kill -9
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 :)

oh thanks..what does the k switch of sort do exactly?

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!

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

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

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}'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.