in reply to Using Perl to find Process Hogs

I've had to do something similar before, and I believe the output of this command should help you:

ps -eo pcpu,pid | sed '1d; s/^ *//; s/  */ /g;' | sort -nr

Update:
One assumption here is that process IDs don't get wrapped around in half an hour, but what are the chances of that?

Another update:
Thinking about it some more, since only one process can consume 75% of CPU at one time, you can append this piece to the pipeline above:


| head -1

Replies are listed 'Best First'.
Re: Re: Using Perl to find Process Hogs
by Octavian (Monk) on Apr 17, 2003 at 18:50 UTC
    What operating system does this work on? -eo, or at least o is not a valid option with ps on our HPUX 11 machine... I actually did explore with ps a bit, and ps -fl or even -ef does give CPU as one of the columns, but the number it gives doesnt make sense to me...our top proccess is taking up about 11% right now, but that number shows 112...so that confused me more than helped, so I gave up on ps ;)

    and to answer your update, yes, more than one proc can have 75% of a CPU, my test box has 2 CPU's, but this will eventually go on machines with 6 ;)

      The TIME CPU column is the total CPU time accumulated by the process. You have to be careful what you mean by "75% of the CPU", because you need to quantify that by saying "over how long of a period". I'm assuming you're going to use some number of seconds or minutes. Thus you would calculate the CPU usage for that period by subtracting the total times for any process that's still running between the two times that you looked, then get the percentage by dividing over the time period you're using.

      Notice this implies your CPU hogs are also long-running. In other words, a CPU-intensive process that goes away in a less than a minute may not register, though your users may still complain about a momentary lack of response.

      Yes, this isn't precise, but it's good enough for government work. ;-)

      Update
      Mislabelled the column name.

      According to 'man ps', -o, and many othe useful options, only work on XPG4 (X/Open Portability Guide v4) compatible boxes...

      --perlplexer
      Six processors... sweet!

      The ps command I'm talking about works on Linux. Of course, Linux's ps supports both BSD and SVR4-type options. On my box, -o lets user specify the columns of output.