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
Shell ScriptingPerlUnix OS

Avatar of undefined
Last Comment
sunhux

8/22/2022 - Mon
SOLUTION
gheist

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gheist

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....
ASKER CERTIFIED SOLUTION
apresence

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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")
              }
Your help has saved me hundreds of hours of internet surfing.
fblack61
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
apresence

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gheist

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!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
gheist

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
apresence

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.
sunhux

ASKER

sorry for the late response.

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

ASKER
excellent
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes