我使用win32serviceutil
中的pywin32
来使用python3.6.4
创建获胜服务,但由于此错误,我无法从服务运行它:
PS C:\pytest> net start MyService2
The service is not responding to the control function.
More help is available by typing NET HELPMSG 2186.
PS C:\pytest> net helpmsg 2186
The service is not responding to the control function.
Error 1053: "The service did not respond in a timely fashion" when attempting to start, stop or pause a service
PS C:\pytest> python.exe .\winservice.py restart
Restarting service MyService2
Traceback (most recent call last):
File ".\winservice.py", line 86, in <module>
win32serviceutil.HandleCommandLine(AppServerSvc)
File "C:\Python\lib\site-packages\win32\lib\win32serviceutil.py", line 609, in HandleCommandLine
RestartService(serviceName, args[1:])
File "C:\Python\lib\site-packages\win32\lib\win32serviceutil.py", line 434, in RestartService
StartService(serviceName, args, machine)
File "C:\Python\lib\site-packages\win32\lib\win32serviceutil.py", line 417, in StartService
win32service.StartService(hs, args)
pywintypes.error: (1053, 'StartService', 'The service did not respond to the start or control request in a timely fashio
n.')
我在windows server 2012 r2
以及windows 10
和windows 7
上测试了代码并始终出现此错误。
这是我的代码:
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import configparser
import os
import inspect
import logging
from logging.handlers import RotatingFileHandler
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "MyService2"
_svc_display_name_ = "My Service2"
_config = configparser.ConfigParser()
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
self._config.read(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + '/config.ini')
print(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
print(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + '/teconfig.ini')
print(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
print(self._config.sections())
logDir = "C:\\pytest"
logPath = logDir + "/service-log.log"
self._logger = logging.getLogger("MyService2")
self._logger.setLevel(logging.DEBUG)
handler = RotatingFileHandler(logPath, maxBytes=4096, backupCount=10)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
self._logger.addHandler(handler)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self._logger.info("Service Is Starting")
self.main()
def main(self):
# your code could go here
rc = None
while rc != win32event.WAIT_OBJECT_0:
print(1)
# mycode ...
# hang for 1 minute or until service is stopped - whichever comes first
rc = win32event.WaitForSingleObject(self.hWaitStop, (1 * 60 * 1000))
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
这是Windows事件日志:
PythonService.exe
3.6.223.0
5a924bf0
pywintypes36.dll
6.3.9600.18821
59ba8666
c0000135
0009d4c2
1c30
01d3be7d982684d6
C:\Python\lib\site-packages\win32\PythonService.exe
pywintypes36.dll
d5d346d8-2a70-11e8-8137-74d4352a14b5
另一个Windows事件日志:
APPCRASH
Not available
0
PythonService.exe
3.6.223.0
5a924bf0
pywintypes36.dll
6.3.9600.18821
59ba8666
c0000135
0009d4c2
0
C:\ProgramData\Microsoft\Windows\WER\ReportQueue\AppCrash_PythonService.ex_a6d37d2a67edc3482c57ce89bdf2468d8fdb_1e95645b_114f887d
d5d346d8-2a70-11e8-8137-74d4352a14b5
4100
这是我的系统路径:
C:\Users\dvp7>echo %PATH%
C:\Python\DLLs\;C:\Python\Scripts\;C:\Python\;C:\Program Files (x86)\Python36-32
\Scripts\;C:\Program Files (x86)\Python36-32\;C:\Program Files\python35\Scripts\
;C:\Program Files\python35\;C:\ProgramData\Oracle\Java\javapath;C:\Windows\syste
m32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1
.0\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program
Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Windows Kits\8
.1\Windows Performance Toolkit\;C:\Program Files\PuTTY\;C:\Program Files\Git\cmd
;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\nodejs\;C:\
Program Files (x86)\Brackets\command;C:\Users\dvp7\AppData\Local\atom\bin;C:\Use
rs\dvp7\AppData\Roaming\npm
我使用此article来创建此代码。
感兴趣的是从powershell以debug
模式运行服务。