Solved

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

Posted on 2011-03-15
5
777 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 180 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 320 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

717 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