简体   繁体   English

使用Perl,如何检查具有给定名称的进程是否正在运行?

[英]Using Perl, how do I check if a process with given name is running or not?

Using Perl, how do I check if a particular Windows process is running or not? 使用Perl,如何检查特定的Windows进程是否正在运行? Basically, I want to start a process using 'exec', but I should do this only if it is not already running. 基本上,我想使用'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模块?

Take a look at the following example that uses the Win32::OLE module. 看一下使用Win32 :: OLE模块的以下示例。 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/^/;

You're probably looking for Proc::ProcessTable (assuming you're using Unix!). 您可能正在寻找Proc :: ProcessTable (假设您使用的是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. 根据您的要求,有相关的软件包可以让您了解各个流程。

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. 也许您无法控制第二个进程,但如果您这样做,那么执行此操作的一个好方法是让进程将其pid( $$ )写入已知位置的文件中。 Then you can read the file and see if that pid exists using kill($pid, 0) . 然后你可以使用kill($pid, 0)读取文件并查看该pid是否存在。

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 ) 来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 拥有句柄时如何检查给定进程是否正在运行 - How to check if a given process is running when having its handle 使用Python,我如何获得该进程的运行进程和exe名称? - Using Python, how can I obtain the running process and the exe name for that process? 批处理-检查给定进程是否正在以管理员权限运行 - Batch - check if given process is running with admin privileges 如何在不知道其名称的情况下检查某个进程是否正在运行 - How to check if a certain process is running without knowing it's name 给定用户名和密码,如何在Windows上使用Perl将文件从一台计算机复制到另一台计算机 - Given username and password, how do I copy files from one machine to another using perl on windows 如何检查给定的字符串是否是 Windows 下的合法/有效文件名? - How do I check if a given string is a legal/valid file name under Windows? 如何检查给定时刻使用麦克风的进程? - How can I check which process uses the microphone at a given moment? 如何检查进程是否在Windows上运行? - How to check if a process is running on Windows? 我如何等待检查以测试进程是否正在运行以完成? - How can I wait for a check to test if a process is running to finish? 如何创建Windows Shorcut来运行Perl脚本? - How do I create a windows shorcut for running a perl script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM