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

I'd like to identify another process's ID and work with it via perl. While I was searching, I came across a Bash answer.
ps ax | awk '/programname/ && !/awk/ {print $1}'
How can I achieve the same result in perl and assign the return value to a simple scalar?

Replies are listed 'Best First'.
Re: Getting another process's pid
by broquaint (Abbot) on Feb 10, 2004 at 12:07 UTC
    If it's supported by your system you could always use Proc::ProcessTable e.g
    my $pt = Proc::ProcessTable->new; print "Found $progname running under $_->{pid}\n" for grep { $_->{pid} != $$ && $_->{cmndline} =~ /$progname/ } @{ $pt->table };
    HTH

    _________
    broquaint

Re: Getting another process's pid
by Abigail-II (Bishop) on Feb 10, 2004 at 12:27 UTC
    If something works for you, it's very Perlish to incorporate that in your Perl program. Perl is, after all, a very succesfull glue language.
    my $pid = `ps ax | awk '/programname/ && !/awk/ {print \$1}`; # ;-)
    Or you could do:
    open my $ps => "ps ax |" or die; my $pid; while (<$ps>) { if (/^\s*(\d+).*\bprogramname\b/) { $pid = $1; last; } } close $ps or die;

    If you happen to run Solaris, you could make use of psgrep instead of ps.

    Abigail