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

hey monks,

i need to get the process id's for all perl processes or every process. i was thinking about running a pipe to 'ps', but parsing the output would be a pain. Is there anyway i can do this?

^jasper <jasper@wintermarket.org>

Replies are listed 'Best First'.
Re: Listing Processes
by zejames (Hermit) on Jun 21, 2002 at 05:17 UTC
    Hi

    You should use Proc::ProcessTable :
    use Proc::ProcessTable; $t = new Proc::ProcessTable; foreach $p ( @{$t->table} ){ print "$p->pid -> $p->cmndline\n"; }

    HTH
    --
    zejames
Re: Listing Processes
by kvale (Monsignor) on Jun 21, 2002 at 05:23 UTC
    The CPAN module Proc::ProcessTable does all the parsing for you. Here is an example from the readme:
    # kill memory pigs use Proc::ProcessTable; $t = new Proc::ProcessTable; foreach $p ( @{$t->table} ){ if( $p->pctmem > 95 ){ $p->kill(9); } }

    -Mark
Re: Listing Processes
by Sinister (Friar) on Jun 21, 2002 at 13:36 UTC
    /me always does
    ps auxw | perl -l -e '@ps = map { chomp; [ split/\s\s+/ ]; } <STDIN>; +shift @ps; print $_->[1],"\t",$_->[$#{$_}] foreach @ps;'
    for that (because it is a shell alias of mine ... )

    But using a module there is just as usefull

    er formait hyarya.
    -- "Life is a house and the next tornado is never far away"
    -- "lovely by nature"