Solved

Unix Script for getting PID of process if Kernel exceeds 80%

Posted on 2009-04-10
4
527 Views
Last Modified: 2013-12-26
I want to write a Unix Shell script which should write the PID of the process with the CPU% it is using into a file when the Kernel utilization increases beyond 80%. How to do that?
0
Comment
Question by:srikanthradix
[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
4 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24118684
Hi,
let's see if I got you right -
You want to examine your system's total cpu utilization and write out the top cpu consuming process along with its PID to a file? The problem I have here: I don't know what you mean with "Kernel utilization" - but
- assuming that you mean "User + System CPU", you could use the following:

#!/bin/ksh
hwm=80numtop=1log=/tmp/tops.log
if [ $(vmstat 1 1 | tail -1 | awk "{print $14 + $15}") -gt $hwm ]
 then
  date >> $log
  ps -ef -o comm -o pid -o pcpu | sort -k3nr | head -$numtop >> $log
  echo "----------------------------" >> $log
 fi
exit

You can adjust the high water mark (hwm=80) and the number of top processes to log (numtop=1)
Configure your logfile by tailoring log=......
I added the command name (-o comm) of the process to the output (along with PID and percent CPU, as you desired).
If you don't need it, remove '-o comm' from the "ps -ef ...' line.

wmp

0
 

Author Comment

by:srikanthradix
ID: 24120111
How to make this script run forever as a daemon? I want to monitor the cpu usage for 2 days.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 24120217
Two possibilities -

1) Create a cron job to run let's say every 15 minutes or so -
00,15,30,45 * * * * /path/to/the/script

2) Run it in background, have it wake up every 15 minutes -

#!/bin/ksh
interval=900
hwm=80
numtop=1
log=/tmp/tops.log
while true do
   if [ $(vmstat 1 1 | tail -1 | awk "{print $14 + $15}") -gt $hwm ]
    then
     date >> $log
     ps -ef -o comm -o pid -o pcpu | sort -k3nr | head -$numtop >> $log
     echo "----------------------------" >> $log
   fi
  sleep $interval done
exit

Configure the desired interval (in seconds) by modifying interval=...
and start the script with

nohup /path/to/the/script &

Please take care to watch the size of your logfile, as it will of course grow over time.

wmp
0
 

Author Closing Comment

by:srikanthradix
ID: 31568987
That is amazing. I have learnt something today. Thanks for answer.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

751 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