• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 799
  • Last Modified:

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


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
sunhux
Asked:
sunhux
  • 2
  • 2
2 Solutions
 
sentnerCommented:
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
 
Alberto CortesCommented:
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
 
sentnerCommented:
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
 
sunhuxAuthor Commented:

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
 
sunhuxAuthor Commented:
Excellent
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now