Lazer
Lazer

Reputation: 94850

What is the equivalent of ps command in perl?

I am using ps -C <executable name> on Linux, but the same does not work on Windows.

How can I perform the same check in Perl so that it is platform independent?

Upvotes: 2

Views: 2257

Answers (1)

Alex
Alex

Reputation: 5893

You might be able to use Win32::Process::List

use 5.12.0;
use warnings;
use Win32::Process::List;

my $P = Win32::Process::List->new();
if($P->IsError == 1) {
    die $P->GetErrorText;
}

my %list = $P->GetProcesses();
foreach my $key (keys %list) {
    # $list{$key} = process name, $key=PID
    say sprintf("%25s %10s", $list{$key}, $key);
}

And process appropriately.

Upvotes: 4

Related Questions