12

所有python代码服务都可以安装但无法启动

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

因为我的服务可以在我的服务器中安装和启动。我认为我的代码没有问题。

但我仍然想知道是否有一个解决方案可以解决代码中的这个错误

我的服务:

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)

“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

更新:

我可以使用调试模式运行此服务,cmd:

memcache_syn_service.py debug
4

7 回答 7

15

使用 pypiwin32(版本:220)和 python(版本:3.6)也有同样的问题。我不得不复制:

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

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

服务启动(在调试模式下工作)

于 2017-03-15T13:48:30.050 回答
13

如果:

  • python your_service.py debug工作,同时
  • python your_service.py install+ 将其作为服务启动失败并出现错误 1053,

这个命令可能会有所帮助python C:\Python27\Scripts\pywin32_postinstall.py

于 2018-07-11T10:12:32.060 回答
4

我所有的 python 编码的 windows 服务都无法在我的计算机上运行。

但是它们都可以从我们的开发服务器开始,这意味着我的代码是正确的。

但我找到了一个替代解决方案,运行debug mode

any_service.py debug
于 2012-12-13T09:53:33.637 回答
2

确保使用与默认本地系统用户不同的用户运行应用程序。将其替换为您能够成功运行调试命令的用户。

  • 要替换用户,请转到 Windows 服务 ( start > services.msc)
  • 右键单击您创建的服务 > 属性 > 登录
  • 取消选中本地系统帐户并输入您自己的。
于 2018-10-09T09:22:14.360 回答
2

所有已知的修复都让我失望了,这个有效:

在服务窗口中:

  1. 右键单击您已安装的服务;
  2. 转到登录选项卡;
  3. 选择“此账户”并输入您的用户名并通过;
  4. 重启电脑。

与我被解释的Windows权限有关......

如果没有为 Windows 用户设置密码,该方法将不起作用。

于 2019-12-01T13:21:20.467 回答
2

就我而言,问题出在python37.dll不在C:\Python37-x64\Lib\site-packages\win32

复制到那里就可以解决问题了

于 2020-12-10T03:19:56.643 回答
1

我在 python 服务中遇到了类似的问题,发现它缺少 DLL,因为“系统路径”(不是用户路径)不完整。检查您的开发服务器中的路径以及它是否与您计算机上的路径匹配(如果服务安装为 LocalSystem 服务,则为系统路径)。对我来说,我错过了 python dll 的路径 c:\python27 (windows)。

于 2013-01-29T10:22:44.063 回答