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

G'day people<br
I am trying to get the process name out of the process table,
Not the fname function some thing that will just give me the name of then process
Such as a.out, dodgy.pl
Things like that.

This is probably a very simple question
But i have try and cannot get it.

the code
$userid = getpwuid($p->uid);
will return the user uid by name
Is it possible to return the pid by name??

Thanks for any help
Chad
thanks mate

Replies are listed 'Best First'.
Re: Proc::ProcessTable Process name,
by sacked (Hermit) on Jun 03, 2004 at 14:53 UTC
    It's not quite clear what you are asking. Are you looking for the full command line of the process? If so, use the cmndline field of the Proc::ProcessTable::Process that you are examining.

    Also, keep in mind that the fields in a Proc::ProcessTable::Process return different values depending on your operating system. From the pod:

    See the "README.osname" files in the distribution for more up-to-date information.

    If you are seeing a full path and only need the filename (basename) of the process, or if you are seeing the name of the interpreter (e.g., "perl" for a process "perl dodgy.pl"), File::Basename can help:
    use strict; use Proc::ProcessTable; use File::Basename; + my $t = Proc::ProcessTable->new; + foreach my $p (@{$t->table}) { print 'process: ', basename($p->cmndline), "\n"; } __END__ for a process `perl /tmp/dodgy.pl', prints `dodgy.pl'

    --sacked
Re: Proc::ProcessTable Process name,
by jdhedden (Deacon) on Jun 03, 2004 at 17:32 UTC
    Processes do not have 'names' per se. The process consists of whatever program + arguments it was invoked with. As mentioned by 'sacked', what Proc::ProcessTable returns is OS dependent. To find out all the fields available from Proc::ProcessTable and what values the current processes have for those fields, try:
    use strict; use warnings; use Proc::ProcessTable; my $t = Proc::ProcessTable->new; my fields = $t->fields(); foreach my $p (@{$t->table}) { foreach my $f (@fields) { print $f, ': ', $p->$f, "\n"; } print "----------\n"; }
    That will give you everything you can potentially work with.