Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Win32 - Dependant services

by Smaug (Pilgrim)
on Oct 21, 2004 at 10:45 UTC ( [id://401117] : perlquestion . print w/replies, xml ) Need Help??

Smaug has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am trying to stop and start MSSQL on a windows server. From the usual console I can click on the "Restart" button and it will stop all dependant services and then stop MSSQL - after that it restarts all the services again and starts MSSQL. Great!
I need to do this in a Perl script but I can not find a module with a "RestartService" method. I have searched on Google too.
Currently I read through the registry and extract the service names and the DependOnService key out and so do it that way.

I was wondering if there is a module to do this that might make my code a little more efficient?
Thanks yet again in advance!

Replies are listed 'Best First'.
Re: Win32 - Dependant services
by periapt (Hermit) on Oct 21, 2004 at 12:17 UTC

    I have had the same issue for several years. I don't know of a Perl solution but there is a system level solution that is simple to implement via perl. You can shell out to windows and use the net stop/net send commands to bring down and bring up the server. The main drawback is that, while you can stop all dependant services with one command, you have to start all the services by hand. I have it set up this way.
    my @services = qw(MSSQLServerOLAPService SQLServerAgent MSSQLSERVER + ) foreach (@services){ my $rtncd = system("net stop $_") >> 8; # some error checking & other code if wanted } foreach (reverse @services){ my $rtncd = system("net start $_") >> 8; # some error checking & other code if wanted }

    The order of the service shutdown and startup is based on the order of ops I have observed on my system. I've occasionally thought of exploring Win32::API to see if I could access the restart option that way but I haven't found the time yet and this solution has worked solidly for many years.

    Hope this helps.

    use strict; use warnings; use diagnostics;
      Hi PJ,
      Here is a possible solution using your command shell idea and the output from 'net stop'
      use strict; my @order; my @results = `net stop mssqlserver /N`; foreach(@results){ if(/^ /){ push(@order,$_) } } foreach(reverse(@order)){ print("Stopping $_\n"); system("net stop \"$_\""); } #...... reverse to start again....

      This is an initial hack. Any ideas for improvement are welcome!!

        Actually, this looks pretty good. I may try it myself. I haven't had the opportunity to take down my server lately so I haven't tested it but I like it.

        use strict; use warnings; use diagnostics;
      Hi PJ,
      That's great....but....I don't always know what the dependant services are. Some servers have HP Insight Manager installed, so that needs to be stopped, and others don't.
      We have got an idea from this and I'll post the code if we get it working.
      Thanks again.
Re: Win32 - Dependant services
by Chady (Priest) on Oct 21, 2004 at 10:53 UTC

    Restarting a service is just like Stopping it, and then Starting it again.

    You might find Win32::Service quite handy.

    update: mis-read the question. this post is irrelevant

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
    Chady |
    Are you a Linux user in Lebanon? join the Lebanese Linux User Group.
      Hi Chady,

      That is what I am currently doing. However if there are services running which are dependant on MSSQL, the service will not stop.

Re: Win32 - Dependant services
by BrowserUk (Patriarch) on Oct 21, 2004 at 18:47 UTC

    Have you tried?

    P:\test>perl -e"system 'net stop mysql'" The MySQL service is stopping. The MySQL service was stopped successfully. P:\test>perl -e"system 'net start mysql'" The MySQL service is starting. The MySQL service was started successfully.

    Update: As a footnote. The NET STOP command is documented to take care of stopping and starting dependant services:

    The syntax of this command is: NET STOP service NET STOP stops Windows services. Stopping a service cancels any network connections the service is using. Also, some services are dependent on others. Stopping one service can stop others. Some services cannot be stopped. service May be one of the following services: ALERTER BROWSER CLIENT SERVICE FOR NETWARE CLIPBOOK DHCP CLIENT FILE REPLICATION MESSENGER NET LOGON NT LM SECURITY SUPPORT PROVIDER REMOTE ACCESS CONNECTION MANAGER ROUTING AND REMOTE ACCESS RPCLOCATOR SCHEDULE SERVER SPOOLER TCP/IP NETBIOS HELPER SERVICE UPS WORKSTATION NET STOP can also stop services not provided with Windows.

    The 3rd paragraph is the relevant one. See the MS docs for what determines whether a service is stoppable or not and what constitutes dependance.

    There seems very little logic in trying to re-create the logic built into these system commands when they are readily usable from Perl?

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      Thanks for your suggestion. However as I said earlier, this service has dependants. If you do as you have suggested above, (but add the /Y switch to answer yes to stopping dependant services) it will stop, but then how would you know what to start again?
      Thanks very much to everybody though for your help. We've decided to use the code I posted earlier, and look into using the API as suggested.

        At least in theory, net start mysql /Y should be able to use the same dependancy logic to re-start those dependant services.

        It certainly used to be able to so under OS/2 when I was developing device drivers. Setting up the dependancy chain for complex drivers, like high level multi-media drivers was a pain, but once set it worked very well.

        I have had little or nothing to do with this under Win32, and don't have any such set of dependant services on this machine to experiment with, so I apologise if the idea was a bum steer.

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon