pha*_*zon 1 perl process monitor
我正在寻找获得Windows机器上正在运行的进程(和服务)列表的最标准方法.重要的是不要使用«modern»东西,因为我会在旧服务器上部署该程序.
任何的想法?
小智 5
正如skp所提到的,tasklist命令可以做到(在Windows XP上测试).
这是一个通过PID创建进程哈希的小脚本:
use warnings;
use strict;
my @procs = `tasklist`;
#Find position of PID based on the ===== ====== line in the header
my $pid_pos;
if ($procs[2] =~ /^=+/)
{
$pid_pos = $+[0]+1;
}
else
{
die "Unexpected format!";
}
my %pids;
for (@procs[3 .. $#procs])
{
#Get process name and strip whitespace
my $name = substr $_,0,$pid_pos;
$name =~s/^\s+|\s+$//g;
#Get PID
if (substr($_,$pid_pos) =~ /^\s*(\d+)/)
{
$pids{$1} = $name;
}
}
use Data::Dumper;
print Dumper %pids;
Run Code Online (Sandbox Code Playgroud)
另一种可能有用的方法是Win32::Process::List.它使用核心Windows C函数获取进程列表.它似乎适用于旧版本的Perl.