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.

In reply to Re: Unable to emulate fork functionality on Windows by cavac
in thread Unable to emulate fork functionality on Windows by perlpal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.