简体   繁体   English

使用Perl轮询进程退出

[英]Polling for process exit using Perl

I am automating some installation procedures using Perl. 我正在使用Perl自动执行一些安装过程。 Now I wish to know when the installation procedure I fired has gotten finished. 现在我想知道我解雇的安装程序何时完成。 How do I do this? 我该怎么做呢? Since this is automation work, I cannot ask people to fire some commands at a later point of time . 由于这是自动化工作,我不能要求人们在以后的某个时间发出一些命令 This functionality should be automatic. 此功能应该是自动的。 How do I do this on Windows? 我如何在Windows上执行此操作?

On Windows, use a handle to the process and call WaitForSingleObject() to find out when the process terminates. 在Windows上,使用进程句柄并调用WaitForSingleObject()以找出进程何时终止。 If you only have the process ID, you can use OpenProcess() to get a handle to it. 如果您只有进程ID,则可以使用OpenProcess()来获取它的句柄。 (Of course, if you created the process yourself with CreateProcess() , you already have a handle to it.) (当然,如果您自己使用CreateProcess()创建了该过程,那么您已经拥有了它的句柄。)

Greg Hewgill's answer addresses the underlying Windows API functions you need, but not how to use them in Perl. Greg Hewgill的答案解决了您需要的基础Windows API函数,但没有解决如何在Perl中使用它们的问题。 You can use the Win32::Process module for this: 您可以使用Win32 :: Process模块:

use strict;
use warnings;

use Win32::Process;

Win32::Process::Create(
  my $process,
  'C:\WINDOWS\system32\notepad.exe', # path of executable
  "notepad",                         # command line it sees
  0,                                 # don't inherit our handles
  NORMAL_PRIORITY_CLASS,             # process creation flags
  "."                                # current directory for process
) or die $^E;

print "started\n";

$process->Wait(INFINITE);

print "done\n";

$process->GetExitCode(my $exitcode) or die $^E;

print "process exit code $exitcode\n";

$process can also be passed to the Win32::IPC functions wait_any and wait_all if you need to wait for more than one object at a time. 如果你需要一次等待多个对象, $process也可以传递给Win32 :: IPC函数wait_anywait_all

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM