6

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?

sebwinadmin
  • 505
  • 2
  • 6
  • 11
  • 3
    Scheduled task to launch a script? `sc stop servicename` and `sc start servicename` No? – HopelessN00b Feb 25 '15 at 14:05
  • 2
    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! – Get-HomeByFiveOClock Feb 25 '15 at 15:13

4 Answers4

4

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.

sebwinadmin
  • 505
  • 2
  • 6
  • 11
3

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.

2

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"

JLmar
  • 21
  • 1
0

You can make a bat file and inside the bat try something like this:

net stop {serviceName} & net start {serviceName}

and use the bat file as a program for a scheduled task.

Iman.G
  • 1