in reply to Unable to emulate fork functionality on Windows

Something like this should work (this is an abbreviated version of my service control class).

Warning, this might/will need some fiddling from you. I pulled this out of my (DarkPAN) repo and removed much of the code to make the (for your problem) relevant parts a bit easier to understand.

First, you need to use some Win32 modules:

use Win32::Process; use Win32;
We'll use a hash reference to store the app's config and runtime data (could also be another module...).
sub start_app { my ($self, $app) = @_; my $ProcessObj; if(!Win32::Process::Create($ProcessObj, $app->{app}, $app->{app}." ".$app->{cmdlineopts}, 0, NORMAL_PRIORITY_CLASS, $self->{basePath})) { print "Error starting app " . $app->{description} . ": " . Win32::FormatMessage( Win32::GetLastError() ) . "\n"; $app->{handle} = undef; return 0; } else { $app->{handle} = $ProcessObj; my $app_pid = $app->{handle}->GetProcessID(); print "Started app " . $app->{description} . " with PID " . $a +pp_pid . "\n"; return 1; } }
You may also want to restart the external application if it quits, so you do something like this every few seconds:
sub check_app { my ($self, $app) = @_; # Ignore if this application was never started # or was intentionally stopped if(!defined($app->{handle})) { return $self->start_app($app); } # First, check if the process exited if($app->{handle}->Wait(1)) { # Process exited, so, restart print "Process exit detected: " . $app->{description} . "!n"; return $self->start_app($app); } # All ok, just return true return 1; }
Ok, you might also want to intentionally kill an external app, either because it hangs or you just don't like its color:
sub stop_app { my ($self, $app) = @_; if(defined($app->{handle}) && $app->{handle}) { print "Killing app " . $app->{description} . "...\n"; my $app_pid = $app->{handle}->GetProcessID(); $app->{handle}->Kill(0); $app->{handle}->Wait(2000); $app->{handle} = undef; print "...killed.\n"; } else { print "App " . $app->{description} . " already killed\n"; } }
I hope the code is useable for you.