Tags: claim, cpu, expert, hog, linux, littlehelp, programming, script, shell, unix, write, writer

CPU Hog script....

On Programmer » Unix & Linux

6,091 words with 2 Comments; publish: Wed, 07 May 2008 11:19:00 GMT; (20093.75, « »)

Hi,

I don't claim to be an expert shell script writer and need a little

help getting this on to work.

I am trying to write a script or if someone has already done it even

better that checks a

certain process lets' call it "build_cache" and when it see's the CPU

over 10% it kills

it. OK, so I wrote a scipt got it to work, put it in the root crontab

and Amen every minute

it kills it. Problem is the cron only runs every minute and I need it

to do it every 30 seconds.

I was trying to turn the script into a loop but could not figure out

how to do it where the

process would just sit on the machine and run every 30 seconds and kill

the "build_cache"

if it saw it. Here is my script if its easy to do and someone could

give me some ideas I'd

be greatful otherwise a minute is better then nothing :)

Thanks in Advance!

Thomas Bishop

========================================

==================

#!/bin/sh

#

# Written by Tom B.for the purpose of killing

# runaway build_cache process that are eating over 10%

# of the CPU, it runs every minute.

#

TMPFILE=/scripts/bc1.log

# add the 'ps' header (only the 1st row of the ps output has "PID" in

the 2nd co

lumn)

###/usr/ucb/ps -aux | awk '$3 == "%CPU"' >> $TMPFILE

chmod 600 $TMPFILE # resetting the 'x' file attribute

#### THIS IS WHERE YOU SET YOU GREP VARIABLE #####

#### VVVVVVVVVVVVV #####

/usr/ucb/ps -aux | grep build_cache.x | grep -v grep | while read

"PROCESS"

do

##### TM=`echo "$PROCESS" | awk '{print $4}'`

TM=`echo "$PROCESS" | awk '{print $3}'`

#####

if [ $TM -ge 10 ] # IF the process > 1

then

chmod 700 $TMPFILE # setting the 'x' file attribute

PID=`echo "$PROCESS" | awk '{print $2}'` # get the PID (awk $2 = 2nd

column)

kill -9 $PID # you could kill the process straight away

/usr/bin/mailx -s "Killed runaway build_cache process '$PID'"

pat.bischoff.unix-linux.todaysummary.com.union

trib.com

# reset the temp file, create the message header text

/usr/bin/date >> $TMPFILE

echo "Following runaway build_cache processes were found and killed\n

'$PID'" >>

$TMPFILE

fi # but be sure to test it carefully before

enabling!!!

done >> $TMPFILE # redirect the output to be appended to the

temp file

All Comments

Leave a comment...

  • 2 Comments
    • On Tue, 14 Mar 2006 15:37:33 -0800, TBishop wrote:

      > Hi,

      > I don't claim to be an expert shell script writer and need a little

      > help getting this on to work.

      > I am trying to write a script or if someone has already done it even

      > better that checks a

      > certain process lets' call it "build_cache" and when it see's the CPU

      > over 10% it kills

      > it. OK, so I wrote a scipt got it to work, put it in the root crontab

      > and Amen every minute

      > it kills it. Problem is the cron only runs every minute and I need it

      > to do it every 30 seconds.

      > I was trying to turn the script into a loop but could not figure out

      > how to do it where the

      > process would just sit on the machine and run every 30 seconds and kill

      > the "build_cache"

      > if it saw it. Here is my script if its easy to do and someone could

      > give me some ideas I'd

      > be greatful otherwise a minute is better then nothing :)

      > Thanks in Advance!

      > Thomas Bishop

      > ========================================

      ==================

      > #!/bin/sh

      > #

      > # Written by Tom B.for the purpose of killing

      > # runaway build_cache process that are eating over 10%

      > # of the CPU, it runs every minute.

      > #

      > TMPFILE=/scripts/bc1.log

      >

      > # add the 'ps' header (only the 1st row of the ps output has "PID" in

      > the 2nd co

      > lumn)

      > ###/usr/ucb/ps -aux | awk '$3 == "%CPU"' >> $TMPFILE

      > chmod 600 $TMPFILE # resetting the 'x' file attribute

      > #### THIS IS WHERE YOU SET YOU GREP VARIABLE #####

      > #### VVVVVVVVVVVVV #####

      > /usr/ucb/ps -aux | grep build_cache.x | grep -v grep | while read

      > "PROCESS"

      > do

      > ##### TM=`echo "$PROCESS" | awk '{print $4}'`

      > TM=`echo "$PROCESS" | awk '{print $3}'`

      > #####

      > if [ $TM -ge 10 ] # IF the process > 1

      > then

      > chmod 700 $TMPFILE # setting the 'x' file attribute

      > PID=`echo "$PROCESS" | awk '{print $2}'` # get the PID (awk $2 = 2nd

      > column)

      > kill -9 $PID # you could kill the process straight away

      > /usr/bin/mailx -s "Killed runaway build_cache process '$PID'"

      > pat.bischoff.unix-linux.todaysummary.com.union

      > trib.com

      > # reset the temp file, create the message header text

      > /usr/bin/date >> $TMPFILE

      > echo "Following runaway build_cache processes were found and killed\n

      > '$PID'" >>

      > $TMPFILE

      > fi # but be sure to test it carefully before

      > enabling!!!

      > done >> $TMPFILE # redirect the output to be appended to the

      > temp file

      Your script was very hard to read because of the mangling that google

      groups did to it (unless of course it was very hard to read in the

      first place!). Perhaps the following will help? It should place a smaller

      load on your CPU.

      #!/bin/sh

      MSG=Killed runaway build_cache process"

      PS=/usr/ucb/ps

      MAILX=/usr/bin/mailx

      while : ; do

      $PS -aux | grep '[b]uild_cache.x' | while read id pid cpu rest

      do

      if [ $cpu -ge 10 ]

      then

      kill -9 $pid

      $MAILX -s "$MSG '$pid'" pat.bischoff.unix-linux.todaysummary.com.uniontrib.com </dev/null

      /usr/bin/date

      echo "runaway build_cache $pid"

      fi

      done

      sleep 30

      done >> $TMPFILE

      #1; Wed, 07 May 2008 11:21:00 GMT
    • Lcarus,

      thanks for the help and Assistance!!!!

      Tom Bishop

      #2; Wed, 07 May 2008 11:22:00 GMT