Capture process time

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My intention in this code is to capture any proccess which is taking more than 60% of CUP time but i am not getting the result. I need suggestions please give your point of view.

[#!/bin/bash
ps uxf > file
cat file |
while read file_info
do
CPU_TIME=`echo $file_info | cut -f 3 -d " "`
CPU_INT=`echo $CPU_TIME | cut -f 1 -d "."`
echo $CPU_INT | grep "%" > /dev/null
if [ $? -ne 0 ]; then
if [ $CPU_INT -gt 60 ]; then
echo $file_info
fi
fi
done][/code]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I had a dog named fifi, i get that. :) The rest is Greek, what language is that, and what is it running on.

It all starts with a mental vision.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

it's a Linux Bash shell script.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That has to be shell on some higher level machine. First line invokes the bash shell.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your script works fine for me on a Debian Linux desktop PC.

Note that a desktop or laptop PC that is not running any CPU-intensive programs is unlikely to have any particular process over 60% CPU usage. So a lot of the time, your script will give no output. I am using a 3.2 GHz dual-core Athlon and as I type this, I am seeing about 2% CPU for a DSP program that runs all the time and listens to the sound card, and sometimes 5% CPU for Firefox for a second or two. Everything else that is running uses 1% or less of the CPU.

You can "artificially" create a process with high CPU usage by saying something like

md5sum /dev/zero

or

gzip < /dev/zero > zero.gz

. Either of these will use nearly 100% CPU. The first one tries to compute a hash of an endless stream of null bytes, while the second one tries to compress an endless stream of null bytes. Press Control-C to stop either one once you are done.

I hope this helps!