12

all python code service can install but cannot start

Error 1053: The service did not respond to the start or control request in a timely fashion".

since my service can install and start in my server. i think my code has no problem.

but i still wonder is there a solution that i can solve this error in code

my service:

import win32serviceutil
import win32service
import win32event

import time
import traceback
import os

import ConfigParser
import time
import traceback
import os
import utils_func
from memcache_synchronizer import *

class MyService(win32serviceutil.ServiceFramework):
    """Windows Service."""
    os.chdir(os.path.dirname(__file__))
    conf_file_name = "memcache_sync_service.ini"
    conf_parser = ConfigParser.SafeConfigParser()
    conf_parser.read(conf_file_name)
    _svc_name_, _svc_display_name_, _svc_description_ = utils_func.get_win_service(conf_parser)

    def __init__(self, args):
        if os.path.dirname(__file__):
            os.chdir(os.path.dirname(__file__))
        win32serviceutil.ServiceFramework.__init__(self, args)

        # create an event that SvcDoRun can wait on and SvcStop can set.
        self.stop_event = win32event.CreateEvent(None, 0, 0, None)

    def SvcDoRun(self):
        self.Run()
        win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.stop_event)
        LoggerInstance.log("memcache_sync service is stopped")
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)
        sys.exit()

    def Run(self):
        try:
            LoggerInstance.log("\n******\n\memcache_sync_service is running, configuration: %s\n******" % (self.conf_file_name,))
            if ((not self.conf_parser.has_section('Memcache')) or
                (not self.conf_parser.has_option('Memcache', 'check_interval'))):
                LoggerInstance.log('memcache_sync_service : no Memcache service parameters')
                self.SvcStop()

            # set configuration parameters from ini configuration
            self.check_interval = self.conf_parser.getint('Memcache', 'check_interval')

            ms = MemcacheSynchronizer()
            while 1:
                ms.Sync()
                time.sleep(self.check_interval)
        except:
            LoggerInstance.log("Unhandled Exception \n\t%s" % (traceback.format_exc(),))


if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(MyService)

execute result of "sc query [name]" cmd:

SERVICE_NAME: NewsMonitoringMemcacheSynchronizer

   TYPE               : 10  WIN32_OWN_PROCESS 
   STATE              : 1  STOPPED 
                           (NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
   WIN32_EXIT_CODE    : 0 (0x0)
   SERVICE_EXIT_CODE  : 0 (0x0)
   CHECKPOINT         : 0x0
   WAIT_HINT          : 0x0

update:

i can run this service with debug mode, cmd:

memcache_syn_service.py debug
Community
  • 1
  • 1
Scott 混合理论
  • 2,263
  • 8
  • 34
  • 59
  • This [answer](https://stackoverflow.com/questions/10556689/error-1053-when-starting-window-service-written-in-python) suggests adding Python to the system PATH. That worked for me – S Anand Oct 15 '17 at 11:44

8 Answers8

15

Had the same problem using pypiwin32 (version: 220) and python (version: 3.6). I had to copy :

"\Python36-32\Lib\site-packages\pypiwin32_system32\pywintypes36.dll"

to

"\Python36-32\Lib\site-packages\win32"

for the service to start (was working in debug mode)

Hossein
  • 24,202
  • 35
  • 119
  • 224
Ken
  • 344
  • 4
  • 12
  • 1
    Worked for me. Thanks! but still can't understand what's gone wrong! – Hasan Baidoun May 02 '17 at 00:03
  • 1
    Worked for me too. But in my case source folder was named as "pywin32_system32". That dll was found in this folder at same path. – Hammad Hassan Sep 14 '18 at 14:16
  • 1
    This worked for me too...what's up with that? We shouldn't have to be moving around DLLs after a `pip` install of `pywin32`. Copied `C:\Python37\Lib\site-packages\pywin32_system32\pywintypes37.dll` to `C:\Python37\Lib\site-packages\win32\pywintypes37.dll` – tsantor May 30 '19 at 16:43
  • I had that problem only on Windows 2019, copying the file pywintypes36.dll solved the issue. – elsadek Nov 15 '21 at 16:27
13

If:

  • python your_service.py debug works, whilst
  • python your_service.py install + start it as a service fails with error 1053,

this command may help python C:\Python27\Scripts\pywin32_postinstall.py.

flam3
  • 1,897
  • 2
  • 19
  • 26
5

all my python coded windows service cannot run on my computer.

but all of them can start at our dev-server which means my code is correct.

but i found a alternative solution, run in debug mode:

any_service.py debug
Scott 混合理论
  • 2,263
  • 8
  • 34
  • 59
  • def Run(self): can be like print "test" one line only? or all those junk of code has to be there? –  Jul 17 '14 at 09:31
2

Make sure you run the application with a different user than the default Local System user. Replace it with the user you successfully be able to run the debug command with.

  • To replace the user go to the windows services (start > services.msc)
  • Right click on the service you created > properties > Log On
  • Uncheck the Local System Account and enter your own.
sjors101
  • 41
  • 3
2

All of the known fixes have failed me, and this one worked:

In services window:

  1. right-click your installed service;
  2. Go to Log On tab;
  3. Select "This Account" and enter your user ID and pass;
  4. Restart PC.

Has to do with Windows Permissions I was explained...

Method won't work if there's no password set for Windows User.

yomajo
  • 106
  • 6
2

In my case the problem was from python37.dll not being at C:\Python37-x64\Lib\site-packages\win32.

Just copy it there and it will solve the problem

user1618465
  • 1,813
  • 2
  • 32
  • 58
1

I had similar problem with a python service and found out that it was missing DLLs since the 'System Path' (not the user path) was not complete. Check the path in your dev-server and whether it matches the one at your computer (System path if service is installed as a LocalSystem service). For me I was missing python dlls' path c:\python27 (windows).

mpaf
  • 6,597
  • 6
  • 38
  • 42
  • this worked for me. although it would help to know what exactly was missing and if it can be included when making an executable with pyinstaller – Ubica Jan 05 '16 at 03:13
0

I had this issue and solved it two times in the same way, simply adding the Environment Variables.

I opened Environment Variables, and in system variable PATH added

C:\Users\MyUser\AppData\Local\Programs\Python\PythonXXX C:\Users\MyUser\AppData\Local\Programs\Python\PythonXXX\Scripts

(Obviously change User name and XXX with Python version)

Rinfra90
  • 24
  • 6