使用 Perl,如何检查具有给定名称的进程是否正在运行? [英] Using Perl, how do I check if a process with given name is running or not?

查看:51
本文介绍了使用 Perl,如何检查具有给定名称的进程是否正在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Perl,我如何检查特定的 Windows 进程是否正在运行?基本上,我想使用exec"启动一个进程,但只有在它尚未运行时才应该这样做.

Using Perl, how do I check if a particular Windows process is running or not? Basically, I want to start a process using 'exec', but I should do this only if it is not already running.

那么如何知道具有特定名称的进程是否正在运行?是否有提供此功能的 Perl 模块?

So how to know if a process with particular name is running or not? Is there any Perl module which provides this feature?

推荐答案

看下面使用 Win32::OLE 模块.它允许您搜索名称与给定正则表达式匹配的正在运行的进程.

Take a look at the following example that uses the Win32::OLE module. It lets you search for running processes whose names match a given regular expression.

#! perl

use warnings;
use strict;

use Win32::OLE qw(in);

sub matching_processes {
  my($pattern) = @_;

  my $objWMI = Win32::OLE->GetObject('winmgmts://./root/cimv2');
  my $procs = $objWMI->InstancesOf('Win32_Process');

  my @hits;
  foreach my $p (in $procs) {
    push @hits => [ $p->Name, $p->ProcessID ]
      if $p->Name =~ /$pattern/;
  }

  wantarray ? @hits : \@hits;
}

print $_->[0], "\n" for matching_processes qr/^/;

这篇关于使用 Perl,如何检查具有给定名称的进程是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆