Thanks for the input. Because of the fact that I started with Perl, I decided to be stubborn and finish in Perl, even though the Sysinternals suite might've made more sense.

Here's what I ended up with:

#!/usr/bin/perl use v5.14; use strict; use warnings; use DBI; use Win32::Service; ## # Hostname of server where this script is being run. # my $hostname = `hostname`; chomp $hostname; ## # Reference to anonymous array returned by GetStatus. # my $w32ServiceStatus = {}; ## # Win32 API service status constants. # use constant { SERVICE_STOPPED => hex('0x00000001'), SERVICE_START_PENDING => hex('0x00000002'), SERVICE_STOP_PENDING => hex('0x00000003'), SERVICE_RUNNING => hex('0x00000004'), SERVICE_CONTINUE_PENDING => hex('0x00000005'), SERVICE_PAUSE_PENDING => hex('0x00000006'), SERVICE_PAUSED => hex('0x00000007'), }; ## # Additional script constants. # use constant { SLEEP_INTERVAL => 15, # Time to sleep bet +ween tries (sec.). MAX_WAIT_TIME => 15, # Max time to wait +for service to stop (min.). SERVICE_NAME => 'MY_SERVICE', # Service name. }; ###################################################################### +# # Get the current status of the service and stop it, if running. # Win32::Service::GetStatus('',SERVICE_NAME,$w32ServiceStatus); if ($w32ServiceStatus->{CurrentState} == SERVICE_RUNNING) { ## # Process ID. # my $pid = -999; ## # WMI database handle object. # my $dbh = DBI->connect('dbi:WMI:'); ## # WMI statement handle object. # my $sth = $dbh->prepare( sprintf ("SELECT * FROM Win32_Service WHERE Name = '%s'", SERVICE_NAME)); $sth->execute(); if (my $row = $sth->fetchrow()) { $pid = $row->ProcessID; } printf("Attempting to stop %s on $hostname...\n",SERVICE_NAME); ################################################################## +# # Stop the service. # Win32::Service::StopService('',SERVICE_NAME) or die sprintf("Unable to stop %s on %s: %s\n", SERVICE_NAME,$hostname,$!); printf("%s on %s stopping.\n",SERVICE_NAME,$hostname); ################################################################## +# # Continue to wait until the process is fully stopped. # my $stopCount = 0; while ($pid > 0 && $stopCount < (60 / SLEEP_INTERVAL * MAX_WAIT_TI +ME)) { sleep SLEEP_INTERVAL; $sth->execute(); if (my $row = $sth->fetchrow()) { $pid = $row->ProcessID; } $stopCount++; } ################################################################## +# # If the time taken to stop exceeds the maximum wait time, then # error out. # if ($stopCount < (60 / SLEEP_INTERVAL * MAX_WAIT_TIME)) { printf("%s stopped on %s.\n",SERVICE_NAME,$hostname); } else { die sprintf("Unable to stop %s on %s: %s\n", SERVICE_NAME,$hostname,$!); } } ###################################################################### +# # Service is not running. # else { printf("%s is not currently running on %s.\n",SERVICE_NAME,$hostna +me); } ###################################################################### +# # ...do whatever we're going to do before restarting the service... # ###################################################################### +# # Restart the service, if it was running to start with. # if ($w32ServiceStatus->{CurrentState} == SERVICE_RUNNING) { printf("Attempting to start %s on %s.\n",SERVICE_NAME,$hostname); Win32::Service::StartService('',SERVICE_NAME) or die sprintf("Unable to start %s on %s.\n",SERVICE_NAME, +$hostname); printf("%s on %s started.\n",SERVICE_NAME,$hostname); }
JAPG (Just Another Perl Grasshopper)

In reply to Re: Validating that service is fully stopped in Windows by DarthBobToo
in thread Validating that service is fully stopped in Windows by DarthBobToo

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.