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

task-schedulerwindows-server-2008-r2windows-service

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?

Best Answer

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.