I created a .cmd file which starts a Python script. I wanted to add it as a Windows service via

C:\Windows\System32>sc create DNSResolver127 binPath= "D:/bin/32dsdnsproxy.cmd"
[SC] CreateService SUCCESS

The service is visible in the Services panel but when i try to start it I get a timeout in the events log (2 events):

A timeout was reached (30000 milliseconds) while waiting for the DNSResolver127 service to connect.

The DNSResolver127 service failed to start due to the following error: 
The service did not respond to the start or control request in a timely fashion.

Despite the 30s timeout the error appears immediately when trying to start the service.

It starts without problems from a CMD prompt.

Are there special requirements for .cmd files when started as a service?

  • D:/bin/32dsdnsproxy.cmd Try using backslashes \ – DavidPostill Jul 12 '19 at 9:09
  • Does your Python script implement the Windows service API (using win32service and win32serviceutil), or is it just... a plain script? – user1686 Jul 12 '19 at 9:23
  • .CMD script cannot be a service. – Akina Jul 12 '19 at 9:27

There are special requirements for everything when started as a Windows service.

The service manager in Windows creates a special control channel to each service, through which it sends stop/pause/resume commands, and through which the service itself must notify the manager that it is "ready". If your Python script didn't do that, the service manager assumes it failed to start and it'll be terminated.

When writing services in Python, you can use win32service and related modules from PyWin32:

Note: You should not need a .cmd script to start the Python script – the binPath can reference the Python interpreter directly.

| improve this answer | |
  • Thank you, I discover the world of Windows services. As for the path, I had issues with spaces in the executable path vs. a space before the arguments but I sorted it out via a separate question. Thank you for your insights. – WoJ Jul 12 '19 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.