How do I create a task scheduler to restart a software service in Windows Server 2008 R2

54,136

Solution 1

Following the suggestions in the comments, I ended up creating a batch file containing the proper restart sequence with timeouts. Timeouts were necessary because of the dependencies between the services. I scheduled it to run as admin every night at 4AM using the task scheduler.

net stop "Service B"
net stop "Service A"
timeout /T 10
net start "Service B"
timeout /T 10
net start "Service A"

It's not ideal, but it will do for this scenario — a remote desktop deployment with less than 10 users.

Solution 2

Instead of creating a bat file, which can become corrupt or missing, you can create a scheduled task with multiple actions. One action to stop the service, and another one to restart the service. Both executed with the NET command. Give them a STOP and START argument, followed by the service name.

NET STOP "Service A" 
NET START "Service A"

Here's a post on StackOverflow explaining how.

Solution 3

Net Stop "ServiceName" && Net Start "ServiceName"

And you can chain together && for Stop/Start ServiceB

Net Stop "ServiceA" && Net Start "ServiceA" && Net Stop "ServiceB" && Net Start "ServiceB"

Share:
54,136

Related videos on Youtube

sebwinadmin
Author by

sebwinadmin

Updated on September 18, 2022

Comments

  • sebwinadmin
    sebwinadmin almost 2 years

    I have a pesky software service that fails every few weeks. It has two components. Service A and Service B. Service B gets in a weird state and stops accepting connections from Service A. The only way out is to restart both services manually, or reboot the server.

    I would like to schedule a service restart for A and B on a regular basis. Say every 24 hours. How to go about it?

    • HopelessN00b
      HopelessN00b over 9 years
      Scheduled task to launch a script? sc stop servicename and sc start servicename No?
    • Get-HomeByFiveOClock
      Get-HomeByFiveOClock over 9 years
      Or run it locally, Net stop "serviceA" && Net start "serviceB" ; but I think the important part is, having to stop and restart a service is not a great solution for anything!
  • SamErde
    SamErde about 8 years
    You need two ampersands in the string above.