You might be able to use Win32::Process.
Here is an example of finding a process (Notepad) by executable name then monitoring it and taking action when it terminates:
use strict;
use warnings;
use Data::Dumper::Concise;
use Win32::Process::List;
use Win32::Process;
my $p = Win32::Process::List->new();
my %list = $p->GetProcesses();
foreach my $pid (keys %list) {
if($list{$pid} eq 'notepad.exe') {
my ($obj, $iflags);
Win32::Process::Open($obj, $pid, $iflags)
or die "Win32::Process::Open failed";
$obj->Wait(INFINITE);
print "Done waiting\n";
exit(0);
}
}
print "Notepad process not found\n";
And here is an example of starting Notepad from Perl, and restarting it when it exits:
use strict;
use warnings;
use Data::Dumper::Concise;
use Win32::Process;
my $obj;
while(1) {
Win32::Process::Create($obj,
"C:\\windows\\system32\\notepad.exe",
"notepad",
0,
NORMAL_PRIORITY_CLASS,
".") or die "Failed to start notepad";
$obj->Wait(INFINITE);
print "Notepad exited - restarting\n";
}
If you want to do something while the process continues to run, you can do that too:
use strict;
use warnings;
use Data::Dumper::Concise;
use Win32::Process;
my $obj;
while(1) {
Win32::Process::Create($obj,
"C:\\windows\\system32\\notepad.exe",
"notepad",
0,
NORMAL_PRIORITY_CLASS,
".") or die "Failed to start notepad";
while(1) {
$obj->Wait(10000);
my $exit_code;
$obj->GetExitCode($exit_code);
if($exit_code == Win32::Process::STILL_ACTIVE()) {
print localtime() . ": Do something while notepad runs\n";
} else {
print localtime() . ": Do something when notepad terminate
+s\n";
last;
}
}
}