Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-04-10
4
Medium Priority
?
538 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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.
Suggested Courses

704 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