Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a windows service that I need to stop and start every 30 minutes using a Windows Scheduled Task.

The service itself can be manually stopped via the Services management console (right-click -> Stop, right-click -> Start).

The following batch file, when run by double-clicking on the file, or through the command line, works almost instantly, and successfully stops and starts the service:

net stop "Service Name"
net start "Service Name"

However, when I create a scheduled task to run this batch file every 30 minutes, I get problems.

When I run the scheduled task (right-click -> Run), the service seems to get stuck in "Stopping" state for about 2 minutes and then remains stopped. The scheduled task seems to report no errors and just sits there waiting for the next 30 minutes.

This is on Windows Server 2008 R2 and the following scheduled task options:

  • Start daily at 08:00:00
  • Repeat every 30 minutes indefinitely
  • If task is already on, don't start another one
  • Allow task to run on demand
  • If task fails, restart every 30 minutes
  • Run even when the user is not logged in
  • Run with the highest privileges
  • Configure for Windows 7, Windows Server 2008 R2

I'm at a loss, is this a known issue? Am I doing something wrong? The only other question that seems similar here is this one: Is there a problem with Windows 7 64-bit stopping service?, but I don't know if my problem is related, and I can reproduce this every time (it never seems to work as a scheduled task, but works fine if I just run the batch file myself)

If you can think of a better question title, please feel free to change!

Not really related to the question, but in case you're interested, the reason I'm doing this is as a temporary work-around for a network-related issue whereby the service (which communicates via a socket connection with another service on a remote machine) is having its port closed due to inactivity every so often. As the network infrastructure can't be changed, the service itself will eventually be updated to ensure it keeps in more regular contact with the remote service.


OK, so for whatever reason, the service was taking too long to stop when my little batch file was run as a scheduled task, so I wrote the following batch file, which waits for it to stop (thanks to @CoreTech for the SC.exe hint)

net stop "service name"

REM Wait for the service to stop (could take a minute or two...)
sc query "service name" | FIND "STATE" | FIND "STOPPED"
if errorlevel 1 goto :timeout_wait

REM Start the service again...
net start "service name"

goto :end

timeout 30
goto :loop_start


This works fine, but I still have no idea why the behaviour is different from just stopping the service manually and setting up the scheduled task :(


Following several hours of introducing my head to a nearby brick wall*, it suddenly dawned on me that there's a third spoke in this ring of infamous Windows Service death...

Two services are involved (I hadn't mentioned this before, because I didn't think it was relevant). It turns out that, because of a misconfiguration, or possibly sabotage, both services are trying to connect over the network on the same port number... The conflict was causing the problems which lead to the need to restart the services in the first place AND was contributing to the odd behaviour when restarting the services. Once I disabled one service and was working with just one service, everything started to work exactly as expected... Moral of the story: Don't just look for the solution, understand the problem and the solution will present itself to you!

I just made that moral up, it's a work in progress...

* Me: "Head, brick wall; brick wall, head."

Head: goes in for a handshake, realises it doesn't have any hands... bang...

Me: "Ow..."

Brick wall: does nothing... it's a brick wall

share|improve this question
I generally assume access rights issues with problems like these. What happens if you set up the task to run under your user account? – Oliver Salzburg Feb 13 '12 at 17:12
Also, redirect the output of the net commands to see if they are reporting errors, like this: net start "Service Name" >> c:\temp\out.txt 2>&1 – CoreTech Feb 13 '12 at 18:16
Suggestion: Try the command SC instead of NET to start, stop and check the state of this service you want to control. – climenole Feb 13 '12 at 18:34
Hey, thanks for the great comments all. I'll try all three suggestions and then write up the answer (if there is one...). – LordScree Feb 14 '12 at 12:06
@OliverSalzburg I'm afraid running the scheduled task under my local admin user account had no effect - the same behaviour occurred.. I will try the other suggestions now. – LordScree Feb 15 '12 at 14:01

You must log in to answer this question.

Browse other questions tagged .