in reply to Stop/start Windows services with perl

I have a script that uses the Win32::Service module to start a service, and it's fairly straightforward to change it to a stop instead. I find it's easier to deal with return codes from a module than a shelled-out "net start" or "net stop".

Have snipped out the important bit:

use Win32::Service; $ServiceName = "SomeService"; sub StartRCS { print "Attempting to start Radia Configuration Server\n"; my %ServiceHash = (); Win32::Service::GetStatus('', $ServiceName, \%ServiceHash); if (%ServiceHash) { if ($ServiceHash{CurrentState} != Win32::Service->SERVICE_RUNN +ING) { if (Win32::Service::StartService('', $ServiceName)) { print "RCS Service start command completed successfull +y"; } else { print "RCS could not be started"; } } else { print "RCS Service already appears to be running"; } } else { print "'$ServiceName' service doesn't exist"; } }

Update: Realised the case of $ServiceName was incorrect.

Hope that helps...

Gordon.

Replies are listed 'Best First'.
Re: (2) Stop/start Windows services with perl
by trplebeam1 (Initiate) on Nov 21, 2003 at 19:33 UTC
    Gordon,

    The application that will utilize this script specifies using only net stop/start commands.

    Thanks for the info though

    Kris

      Hmmm... well in that case, I would use something like:

      @rc = `net stop messenger`; if ($rc[1] =~ /success/) { print "Messenger stopped successfully\n"; } else { print "Problem occurred stopping Messenger\n"; } @rc = `net start messenger`; if ($rc[1] =~ /success/) { print "Messenger started successfully\n"; } else { print "Problem occurred starting Messenger\n"; }

      You might also want to run `net start` on its own first and search for the name of the service you're about to start or stop -- that way you'll know whether it's running.

      Gordon