Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

885 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