[comp.lang.perl] Scanning "ps" output with Perl

ted@evi.com (Ted Stefanik) (05/03/91)

I've noticed that several Perl utilities scan "ps" output using splits
and/or regular expressions.  I'd like to add some cautions to these
approaches.

When a particular process's statistics numbers get too big for the column space
alloted to that number, ps just slams the number into the preceeding column.

For example, this looks nice:

USER       PID %CPU %MEM   SZ  RSS TT STAT  TIME COMMAND
ted      18374 51.2  3.0  488  380 p0 R     0:00 ps auxww

If the process got memory hoggy and CPU hoggy, and was niced and not currently
active, you'd get:

USER       PID %CPU %MEM   SZ  RSS TT STAT  TIME COMMAND
ted      18374 51.2  3.012488 1380 p0 IWN2345:45 ps auxww

Notice that the size and time fields blend into the previous fields.

In my opinion, splitting is out.  You have to be very careful with regular
expressions, because the white space is not always gauranteed to be there.  I
use substr, but that would also fail if the fields overflow one more place:

USER       PID %CPU %MEM   SZ  RSS TT STAT  TIME COMMAND
ted      18374 51.2  3.0124888 1380 p0 IWN12345:45 ps auxww

(Although I both these are unlikely on my machine - 65MB swap space with
weekly reboots.)


*************************************************************************
*   Ted Stefanik           (Electronic Component Information Systems)   *
*   Expert Views                                                        *
*   100 Fifth Avenue       ted@evi.com                                  *
*   Waltham, MA  02154     ted%evi.com@uu.psi.com                       *
*   (617) 890-0333         uupsi!blt!ted                                *
*************************************************************************

tchrist@convex.COM (Tom Christiansen) (05/04/91)

From the keyboard of ted@evi.com (Ted Stefanik):
:I've noticed that several Perl utilities scan "ps" output using splits
:and/or regular expressions.  I'd like to add some cautions to these
:approaches.
:
:When a particular process's statistics numbers get too big for the column space
:alloted to that number, ps just slams the number into the preceeding column.


This is unfortunately pretty OS dependent.  Some versions (mine, for
example) seems to guarantee a space between fields, even if some fields
should get wide.

    USER       PID %CPU %MEM   SZ  RSS TT  STAT   TIME COMMAND
    tchrist    550 53.1  0.1 6508  464 o2  R      0:01 ps axu
    user0    29596  0.0 12.6 65568 64300 q1  T N   10:34 program
    user1    22172 52.4 31.2 278648 78840 pc VR N   100:14 another_program

The only thing here mildly difficult is finding all the STAT stuff, which
can contains blanks.


--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
		"So much mail, so little time."