none
Stopping remote services before windows shutdown RRS feed

  • Question

  • I am looking for a way that when someone reboots the server that I can stop some services on a remote server before the shutdown happens. I am have playing the the Shutdown script in group policies but can't see to get it to work. One of the problems I am running into is I need to restart the service accounts with specific username and passwords. I have a SQL Server and I want to stop the application before the SQL server reboots. How is everyone handling this?

    Eric

    Thursday, August 30, 2018 8:08 PM

Answers

  • Then try adding your script here.

     

     

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    • Marked as answer by edycus Thursday, September 6, 2018 9:09 PM
    Thursday, August 30, 2018 9:37 PM

All replies

  • A couple of methods.

    • sc \\machinename stop <service>
    • https://docs.microsoft.com/en-us/sysinternals/downloads/psexec

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Thursday, August 30, 2018 8:23 PM
  • I guess I need the clarify. I am looking for an automated way to stop some remote service when anyone reboots or shutdown the server. The problem I have is, how to get Windows to run some commands that require network access it runs as different user accounts when it starts shutting down. I have tried calling a task scheduler from the shutdown script but the task scheduler says error "shutdown in progress". 

    The closest I have come is using PowerShell script and the GPO for the Shutdown command. By passing it some encrypted credentials I was able to use invoke-command and stop the service on the remote server. This is a bit more complicated then I would like it.

    Is there any other way?




    • Edited by edycus Thursday, September 6, 2018 5:28 PM
    Thursday, August 30, 2018 9:31 PM
  • Then try adding your script here.

     

     

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    • Marked as answer by edycus Thursday, September 6, 2018 9:09 PM
    Thursday, August 30, 2018 9:37 PM
  •  Dave, I have looked at the that and the challenge is the script runs as the system account and need the scripts to run as specific service accounts.   


    • Edited by edycus Friday, August 31, 2018 8:24 PM
    Friday, August 31, 2018 4:30 PM
  • Development issues are better addressed somewhere on MSDN, possibly over here.

    https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/home?forum=windowsgeneraldevelopmentissues

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Friday, August 31, 2018 4:37 PM
  • I am not sure why you said development. My general question how are people doing this today? To cleanly close when an application when the database server shutdown occurs. 

    • Edited by edycus Friday, August 31, 2018 8:24 PM
    Friday, August 31, 2018 8:23 PM
  • For services this happens natively. If you wanted more time then you can increase the WaitToKillServiceTimeout value.

     

     https://docs.microsoft.com/en-us/windows/desktop/services/service-control-handler-function

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.




    Friday, August 31, 2018 8:36 PM
  • Hi,

    How things are going there on this issue?

    Please let me know if you would like further assistance.

    Best Regards,
    Eve Wang

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, September 5, 2018 2:18 AM
    Moderator
  • Hi,

    Is there any update?

    Please click “Mark as answer” if any of above reply is helpful. It would make this reply to the top and easier to be found for other people who has the similar problem.

    Best Regards,
    Eve Wang

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, September 6, 2018 1:57 AM
    Moderator
  • I was working with the Shutdown script command and it runs as the System account and when I try to invoke the process as a different user it gives me access is denied. I wonder if there is a built-in security to prevent the system account from using impresonilazation.  

    When patching happens the server reboots. I want the server to shutdown the application services before it automatically reboots. I'm wondering if there is an easier way to do this or if it is possible?  How does your SQL server handle reboots? Does it stop the application services first before rebooting? 


    • Edited by edycus Thursday, September 6, 2018 5:27 PM
    Thursday, September 6, 2018 4:23 PM
  • If your application needs services to shutdown in some specific order you may be able to incorporate SetProcessShutdownParameters function in your app.

    https://docs.microsoft.com/en-us/windows/desktop/services/service-control-handler-function

     Services do not control application. Stopping an application would need some other method.

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.


    Thursday, September 6, 2018 4:29 PM
  • I don't have an app but maybe I need one. LOL In way to do this in Powershell?

    Thursday, September 6, 2018 5:23 PM
  • I'd try asking about this over here.

    https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/home?forum=windowsgeneraldevelopmentissues

    https://social.technet.microsoft.com/Forums/en-US/home?forum=winserverpowershell

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Thursday, September 6, 2018 6:33 PM
  • Thanks Dave, I didn't get the answer I was looking for but I'll consider this closed. 

    My solution I think I will end up with is. Use Shutdown in the local group policy to run a batch file.

    For this to work I will have to add the system account of the SQL server to the application server so it has rights to the services to start and stop. 

    Thursday, September 6, 2018 9:09 PM
  • Sounds good, you're welcome. If all you're concerned about is the mssqlserver service then you should not have to do anything as the OS will shut it down for you.

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Thursday, September 6, 2018 9:17 PM
  • No, its the application services I want to stop on a remote server before the SQL reboots itself.
    Thursday, September 6, 2018 10:40 PM
  • Might try a PreshutdownOrder value.

    To set the shutdown order of services manually, create a multistring registry value that contains the service names in the order in which they should be shut down and assign it to the Control key's PreshutdownOrder value, as follows:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\PreshutdownOrder="Shutdown Order"

    https://docs.microsoft.com/en-us/windows/desktop/services/service-control-handler-function

     

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Thursday, September 6, 2018 11:06 PM