How do I create a task scheduler to restart a software service in Windows Server 2008 R2
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"
Related videos on Youtube
sebwinadmin
Updated on September 18, 2022Comments
-
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 over 9 yearsScheduled task to launch a script?
sc stop servicename
andsc start servicename
No? -
Get-HomeByFiveOClock over 9 yearsOr 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 about 8 yearsYou need two ampersands in the string above.