Solved

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

Posted on 2011-03-15
5
767 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
  • 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

831 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