Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polling for process exit using Perl

Tags:

windows

perl

I am automating some installation procedures using 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?

like image 467
Chani Avatar asked Aug 22 '26 14:08

Chani


2 Answers

On Windows, use a handle to the process and call WaitForSingleObject() to find out when the process terminates. If you only have the process ID, you can use OpenProcess() to get a handle to it. (Of course, if you created the process yourself with CreateProcess(), you already have a handle to it.)

like image 192
Greg Hewgill Avatar answered Aug 24 '26 04:08

Greg Hewgill


Greg Hewgill's answer addresses the underlying Windows API functions you need, but not how to use them in Perl. You can use the Win32::Process module for this:

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.

like image 41
cjm Avatar answered Aug 24 '26 03:08

cjm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!