Link to home
Create AccountLog in
Avatar of sunhux
sunhux

asked on

Unix Shell script or Perl that checks for high CPU process & renice it

I need a Perl or Shell script that will poll, say the top 10 CPU processes
given by "top" every 30 secs & if after polling 8 times & a particular
process CPU consumption is above 70% for all the 8 polls, then it will
renice the process to a friendlier priority.

Below is one script but it's not quite what I wanted


Preferably this script runs from crontab : if in cron we can only set to
run every minute, then this script will have to run twice per minute,
say by placing a "sleep 30 secs" ?

attached a sample but it's not quite I wanted
a.txt
SOLUTION
Avatar of gheist
gheist
Flag of Belgium image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
PS your system probably contains "process aging" facility which reduces PRI (not NICE) so that long-lived processes do not consume too much.
PPS i suggest to let it run and watch closely. one runaway process probably is caused by bottleneck somewhere else...
e.g missing MySQL backend index and build_cache scanning table which now is big many times....
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of sunhux
sunhux

ASKER


Thanks chaps.


Hi APresence,

Excellent script.  If I may request for 2 enhancements to your script:

1)  renice only top CPU processes with names that have the sub-string
    "AWSERVICES" OR "javaw" in them only & not any other processes

2) to append to a logfile the processes that have been reniced & time/date
    they were reniced.  I thought of amending the "system ..." section as follows
    but let me know if I've amended the codes below correctly:
              {
              print "Process $pid cpu utilization has met threshold, nicing ...\n";
              # Do your nice command here, ex:
              system("renice $nice_level -p $pid");
              system("echo 'Process' $pid ' reniced at ' $date >> /var/tmp/renicedpid.log")
              }
Avatar of sunhux

ASKER


>  top CPU processes with names that have the sub-string    "AWSERVICES" OR "javaw"
Just to elaborate, I meant processes with above sub-string as given by "ps -ef" or "ps -aux"
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
if you say which operating system you are using (uname -a)
I will assist on tuning process aging correctly so that it ages processes soon enough to make "control" script opsolete.
Deal?
@sunhux: You might want to give gheist a chance to elaborate on his idea.  I'm curious myself.  He needs the output of your "uname -a" command.
@gheist: Based on his sample script which references /usr/ucb and his user name, I'd say it's a safe bet he's running on Solaris.  Not sure what version, however.
@sunhux: Please assign points after we have an answer from gheist.  Thanks!
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Please note that the page that gheist posted is not easily readable using IE8 (one word per line... very hard to read).  Looks good in FireFox, however.
Avatar of sunhux

ASKER


sorry for the late response.

Basically I have 2 OS : RHES 4.6 and HP-UX B11.11
Avatar of sunhux

ASKER

excellent