?
Solved

Shell script to check for a process CPU util & then thread dumps it if it exceeds 30%

Posted on 2011-03-15
5
Medium Priority
?
786 Views
Last Modified: 2012-08-13

Need to run a script every 15 mins in crontab :
0,15,30,45 * * * *  the_script

I think my HP-UX version doesn't support the followg:
ps -eo pid,comm,%cpu


So need a script to grab the %CPU column (think it's 9th or
10th column in top in HP-UX) & if the application concerned
consumes more than 30%, it will thread dump that process :

top –n 99 -f /tmp/topcpu.txt
cpuval=`grep  "myapp" /tmp/topcpu.txt |grep java | grep -v grep| awk or sed
         for the CPU column`

if cpuval > 30  then
   {
     get the pid of myapp from 'ps -ef | grep myapp | grep java | grep -v grep'
     kill -3 $pid
     $DATE > /tmp/log1.txt
     sleep 30  # sleep 30 secs
     kill -3 $pid
     sleep 30
     kill -3 $pid
     echo $cpuval >> /tmp/log1.txt
     mailx -s "myapp cpu utilizn exceed 30%"  myemail@xx.com < /tmp/log1.txt
     mailx ... the threadump file ... to myemail
  }
0
Comment
Question by:sunhux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 14

Assisted Solution

by:sentner
sentner earned 720 total points
ID: 35139364
You should be able to use the standard ps output, but will need to specify to use the standard berkely syntax.


export UNIX95=yes
cpuval=`ps -eo ps -eo pid,comm,pcpu | grep "[m]yapp" | grep java | awk '{print $3}'`


0
 
LVL 3

Accepted Solution

by:
Alberto Cortes earned 1280 total points
ID: 35140439
Solution:

To create the file topcpu.txt use next command:
UNIX95= ps -e -o "pcpu pid args" | sort -rn | head -99 > /tmp/topcpu.txt

For cpuval use next command:
cpuval=$(cat /tmp/topcpu.txt | grep myapp | awk '{print $1}')

0
 
LVL 14

Expert Comment

by:sentner
ID: 35142642
Rico, your solution adds nothing that mine didn't already do, and in fact it adds extra commands that aren't needed.

There's no need to push this first to a file, nor to sort it or to limit it to 99 lines in the ps output (in fact that may cause you to miss what you're looking for).  

The key is the UNIX95 setting.  That can either be set by adding "UNIX95= " (the space after the '=' is important) or by explicitly setting it using "export UNIX95=yes".  I chose the latter, as that's more readable, and this is going into a script.

0
 

Author Comment

by:sunhux
ID: 35144940

RicoCebiche solution is fine.



> cpuval=`ps -eo ps -eo pid,comm,pcpu | grep "[m]yapp" | grep java | awk '{print $3}'`
Sentner's solution has slight issue :
"ps -eo" occurred twice & by doing "ps -ef pid,comm,pcpu" too early
the grep "myapp" & grep "java" would not be able to find the search string
as the process/application name columns were truncated too early
0
 

Author Closing Comment

by:sunhux
ID: 35149641
Excellent
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question