js1 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Does anyone know if there's a perl equivalent for 'ps'? I want to run 'ps -ef' on Linux, AIX, and Solaris for an audit script I'm writing. However, on Solaris, it truncates the command arguments to the first 80 bytes.

It would be good to have a ps command which we could run anywhere, and get the same output.

Thanks,

js1.

Replies are listed 'Best First'.
Re: perl function for 'ps' ?
by gellyfish (Monsignor) on Aug 14, 2006 at 10:51 UTC
Re: perl function for 'ps' ?
by davorg (Chancellor) on Aug 14, 2006 at 10:51 UTC

      Thanks for your reply. I do know of cpan. I was actually looking for a way to do this without having to install a module. I can't install this on all the boxes I'm auditing.

        If you can get logged in and run perl or ps, then you might very well be able to install a module in your $HOME directory and use the module from there.
        You have more power than you might think!
        Check out this node.
Re: perl function for 'ps' ?
by derby (Abbot) on Aug 14, 2006 at 12:42 UTC

    on Solaris, it truncates the command arguments to the first 80 bytes

    Well ... you could always just recognize you're on solaris and use /usr/ucb/ps -auxw instead of /bin/ps -ef

    -derby
      And could use 'uname' to check the OS, for example:
      my $os = `uname`; chop $os; my $cmd = ( $os eq 'SunOS' ) ? '/usr/ucb -auxw' : '/bin/ps -ef'; my @psout = split /\n/, `$cmd`; my @hdr = split /\s+/, shift ( @psout ); my @aoh = (); while (1) { # load ps output into above array of hash... my $fldno = 0; my $tmp = {}; my @fld = ( split /\s+/,( shift( @psout ) || last )); $tmp -> { $hdr [ $fldno++ ] } = shift @fld for @hdr; $tmp -> { COMMAND } .= join ' ', @fld; push @aoh, $tmp; }

      -M

      Free your mind

        And yet another Perl-ish way to check the OS is to look at $^O (or if you use English, you can check it as $OSNAME). For instance something like the following would work:

        use English; BEGIN { if ( $OSNAME =~ /MSWin32/i) { #do some stuff for Windows } elsif ( $OSNAME =~ /solaris/i) { #do some stuff for solaris }# }#end BEGIN
        You could put in whatever the various OSes are that you would need.

        I'm not sure there is any advantage in this case in using `uname` over $^O here, $^O does however contain "solaris" rather than "SunOS" on the Solaris machine I have to test with.

        /J\

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: perl function for 'ps' ?
by Khen1950fx (Canon) on Aug 14, 2006 at 11:58 UTC
Re: perl function for 'ps' ?
by planetscape (Chancellor) on Aug 15, 2006 at 20:29 UTC