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,如何检查特定的Windows进程是否正在运行?基本上,我想使用'exec'启动一个进程,但是只有当它还没有运行时我才应该这样做。
So how to know if a process with particular name is running or not? Is there any Perl module which provides this feature?
那么如何知道具有特定名称的进程是否正在运行?有没有提供此功能的Perl模块?
4 个解决方案
#1
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.
看一下使用Win32 :: OLE模块的以下示例。它允许您搜索名称与给定正则表达式匹配的正在运行的进程。
#! 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/^/;
#2
You're probably looking for Proc::ProcessTable (assuming you're using Unix!). It gives you access to the list of processes, and you can query its fields to find the process with the name. There are related packages to allow you to get at individual processes, depending what you want to do.
您可能正在寻找Proc :: ProcessTable(假设您使用的是Unix!)。它使您可以访问进程列表,并且可以查询其字段以查找具有名称的进程。根据您的要求,有相关的软件包可以让您了解各个流程。
#3
Maybe you don't have control over the second process, but if you do, a good way to do this is to have the process write its pid ($$
) out to a file in a known location. Then you can read the file and see if that pid exists using kill($pid, 0)
.
也许您无法控制第二个进程,但如果您这样做,那么执行此操作的一个好方法是让进程将其pid($$)写入已知位置的文件中。然后你可以使用kill($ pid,0)读取文件并查看该pid是否存在。
#4
What you really want is a way to stop a process from running if it is already running (what if you have two different programs with the same name, or decide to name your program explorer.exe?) This works for me on Linux:
你真正想要的是一种阻止进程运行的方法,如果它已经在运行(如果你有两个不同的程序具有相同的名称,或者决定命名你的程序explorer.exe怎么办?)这适用于Linux:
use Fcntl ':flock';
open SELF, '<', $0 or die 'I am already running...';
flock SELF, LOCK_EX | LOCK_NB or exit;
In my testing that code does not want to be in any block.
在我的测试中,代码不希望出现在任何块中。
(source)
#1
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.
看一下使用Win32 :: OLE模块的以下示例。它允许您搜索名称与给定正则表达式匹配的正在运行的进程。
#! 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/^/;
#2
You're probably looking for Proc::ProcessTable (assuming you're using Unix!). It gives you access to the list of processes, and you can query its fields to find the process with the name. There are related packages to allow you to get at individual processes, depending what you want to do.
您可能正在寻找Proc :: ProcessTable(假设您使用的是Unix!)。它使您可以访问进程列表,并且可以查询其字段以查找具有名称的进程。根据您的要求,有相关的软件包可以让您了解各个流程。
#3
Maybe you don't have control over the second process, but if you do, a good way to do this is to have the process write its pid ($$
) out to a file in a known location. Then you can read the file and see if that pid exists using kill($pid, 0)
.
也许您无法控制第二个进程,但如果您这样做,那么执行此操作的一个好方法是让进程将其pid($$)写入已知位置的文件中。然后你可以使用kill($ pid,0)读取文件并查看该pid是否存在。
#4
What you really want is a way to stop a process from running if it is already running (what if you have two different programs with the same name, or decide to name your program explorer.exe?) This works for me on Linux:
你真正想要的是一种阻止进程运行的方法,如果它已经在运行(如果你有两个不同的程序具有相同的名称,或者决定命名你的程序explorer.exe怎么办?)这适用于Linux:
use Fcntl ':flock';
open SELF, '<', $0 or die 'I am already running...';
flock SELF, LOCK_EX | LOCK_NB or exit;
In my testing that code does not want to be in any block.
在我的测试中,代码不希望出现在任何块中。
(source)