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
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...
gheist
e.g missing MySQL backend index and build_cache scanning table which now is big many times....
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")
}
> 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"
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?
apresence
@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!
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.
PPS i suggest to let it run and watch closely. one runaway process probably is caused by bottleneck somewhere else...