The Core Technologies Blog

Our Software // Windows Services // 24×7 Operation


Q&A: How do I Restart my Windows Service at a Specific Time every Week?

Q&A - Restart Windows Service at a Specific Time
  I want to restart some specific service for a specific time every week. Can I use the NET command?

— Shanmuga

Hi Shanmuga.

Yes. With the help of the Windows Task Scheduler, you can use the NET command to restart a specific service at a specific time.

To do so:

1. Find the name of your service

Each Windows Service has two names — a short service name and a friendly display name. We need the service name for the NET command.

If you don’t already know the service name, or want to validate it:

  1. Launch the Windows Services application. You can find it by searching for “services” in the Control Panel, or by running services.msc at a command prompt.

  2. Scroll to locate your service in the list:

    Windows Services application
  3. Double-click the entry to open the service’s properties. The service name is displayed at the top.

Here we see that the name of the Print Spooler service is actually “Spooler”:

Print Spooler service name

2. Create a batch file to restart your service

With the service name in hand, we can now use the NET command to restart the service.

Create a new batch file and enter the following two commands:

NET STOP "Your Service Name"
NET START "Your Service Name"

Please replace Your Service Name with the service name identified in step 1. The quotes are required if the service name contains a space.

For example, if your service name is “Spooler”, the batch file should look like this:

NET STOP "Spooler"
NET START "Spooler"

Save the batch file to a well-known location. We’ll use it in the next step.

Test the batch file

At this point, we recommend performing a quick test to ensure that the batch file works as expected. Run it from an administrative command prompt and confirm that it restarts your service.

3. Create a scheduled task to run the batch file at the time you wish to restart the service

Now that you are able to restart the service with the batch file, let’s schedule it to run whenever you like.

For example, here is how we would restart the Print Spooler service every Sunday at 1 AM:

  1. Open the Windows Task Scheduler. You start it from the Control Panel or by running taskschd.msc from a command prompt.

  2. Click Create Basic Task on the right:

    Task Scheduler: Create Basic Task

    The Create Basic Task Wizard window will come up.

  3. Give the task a descriptive name:

    Enter the task name

    Click Next to continue.

  4. Select Weekly and click Next:

    Set the task to run weekly
  5. Set the day and time to restart the service:

    Set the day and time to restart the service

    Click Next to continue.

  6. Ensure that the action is Start a program and move to the next step:

    Set the action - Start a program
  7. Enter the full path to the batch file you created to restart the service:

    Specify the batch file to restart the service

    Click Next to continue.

  8. Review the summary and make sure that everything looks good.

    Check the Open the Properties dialog… box at the bottom because we’ll need to adjust one of the tasks properties:

    Review the summary of the task

    Click Finish.

  9. And finally, in the Properties window, check the Run with highest privileges box. The batch file must run with administrative rights so that it can restart the service.

    Run the task with highest privileges

    Click OK to save your settings.

Going forward, the new task will come alive at the scheduled time to promptly restart the service. You should be good to go.

Improvement: Use ServicePilot instead of NET for better reliability

Use ServicePilot instead of NET.EXE

While NET.EXE will work for most situations, there are a few scenarios where it may fall short.

Does your service:

  • take longer than 30 seconds to shut down?
  • occasionally hang and refuse to stop?

If so, the NET STOP command may fail. And when that happens, the subsequent call to NET START will fail too (because the service will not be idle). The end result is that your service will be left in an unusable/unresponsive state!

Our free ServicePilot utility was built to work around NET’s shortcomings. It will wait for longer than 30 seconds if necessary and will do its best to forcibly terminate an unresponsive service.

To use ServicePilot instead of NET:

  1. Download ServicePilot from our website. Save the executable in a well-known location (e.g. C:\Apps\ServicePilot\ServicePilot.exe).

  2. Edit the batch file you created to restart the service.

  3. Delete the two NET lines.

  4. Add the following line (adjusting the full path to ServicePilot and your service name as necessary):

    C:\Apps\ServicePilot\ServicePilot.exe -restart -wait 300 "Your Service Name"

    Note: The -wait 300 parameter instructs ServicePilot to wait up to 300 seconds (5 minutes) for the service to stop and restart. Feel free to increase (or decrease) the timeout based your specific use case.

  5. Save the batch file.

As you did with the NET version, please perform a quick test to ensure that the updated batch file works as expected. Launch it from an administrative command prompt and confirm that it restarts your service.

Best of luck with your service!

Posted in Windows Services | Tagged , , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *