Error 1053 When Starting Window Service Written In

2019-08-20 03:19发布

问题:

I have already looked at and tried the resolutions to this question that others have posted. One user said that to try and change my setup.py file from:

from distutils.core import setup
import py2exe

setup(console=["dev.py"])

to

from distutils.core import setup
import py2exe

setup(service=["dev.py"])

I got the following results:

running py2exe
*** searching for required modules ***
Traceback (most recent call last):
  File "C:\Python27\Scripts\distutils-setup.py", line 5, in <module>
  setup(service=["C:\Python27\Scripts\dev.py"])
File "C:\Python27\lib\distutils\core.py", line 152, in setup
  dist.run_commands()
File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
  self.run_command(cmd)
File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
  cmd_obj.run()
File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 243, in run
  self._run()
File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 296, in _run
  self.find_needed_modules(mf, required_files, required_modules)
File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 1274, in
find_needed_modules
  mf.import_hook(mod)
File "C:\Python27\lib\site-packages\py2exe\mf.py", line 719, in import_hook
  return Base.import_hook(self,name,caller,fromlist,level)
File "C:\Python27\lib\site-packages\py2exe\mf.py", line 136, in import_hook
  q, tail = self.find_head_package(parent, name)
File "C:\Python27\lib\site-packages\py2exe\mf.py", line 204, in find_head_package
  raise ImportError, "No module named " + qname
ImportError: No module named dev

Now, when I run py2exe with "console" in my setup script it works fine, but the service doesn't start and I get the error. When I run py2exe with "service" in my setup script py2exe doesn't run and tells me it can't find my module.

I have tried to re-install py2exe to no resolution. I have also tried to change:

def SvcDoRun(self):
    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                          servicemanager.PYS_SERVICE_STARTED,
                          (self._svc_name_,''))

to

def  SvcDoRun(self):
     self.ReportServiceStatus(win32service.SERVICE_RUNNING)
     win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

Didn't make a difference either. CAN ANYONE HELP ME PLEASE? Here is what I am working on. It monitors a server and spits back a text file every 60 seconds which I use to monitor my servers at any given minute. Any help you guys and gals can give would be great.

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import wmi
import _winreg
from time import sleep
import os

class SrvMonSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "SrvMonSvc"
_svc_display_name_ = "Server Monitor"

def __init__(self,args):
    win32serviceutil.ServiceFramework.__init__(self,args)
    self.hWaitStop = win32event.CreateEvent(None,0,0,None)
    socket.setdefaulttimeout(60)

def SvcStop(self):
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    win32event.SetEvent(self.hWaitStop)

def SvcDoRun(self):
    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                          servicemanager.PYS_SERVICE_STARTED,
                          (self._svc_name_,''))
    self.main()

def main(self):
    host = wmi.WMI(namespace="root/default").StdRegProv
    try:
        result, api = host.GetStringValue(
        hDefKey = _winreg.HKEY_LOCAL_MACHINE,
        sSubKeyName = "SOFTWARE\Server Monitor",
        sValueName = "API")
        if api == None:
            raise Exception
        else:
            pass
    except:
        exit()

    while 1 == 1:
        with open("C:/test.txt", "wb") as b:
            computer = wmi.WMI(computer="exsan100")
            for disk in computer.Win32_LogicalDisk (DriveType=3):
                name = disk.caption
                size = round(float(disk.Size)/1073741824, 2)
                free = round(float(disk.FreeSpace)/1073741824, 2)
                used = round(float(size), 2) - round(float(free), 2)
                for mem in computer.Win32_OperatingSystem():
                    a_mem = (int(mem.FreePhysicalMemory)/1024)
                for me in computer.Win32_ComputerSystem():
                    t_mem = (int(me.TotalPhysicalMemory)/1048576)
                    u_mem = t_mem - a_mem
                for cpu in computer.Win32_Processor():
                    load = cpu.LoadPercentage
                print >>b, api
                print >>b, name
                print >>b, size
                print >>b, used
                print >>b, t_mem
                print >>b, u_mem
                print >>b, load
        b.close()
        date_list = []
        stamp = time.strftime("%c",time.localtime(time.time()))
        date_list.append(stamp)
        name = re.sub(r"[^\w\s]", "",date_list[0])
        os.rename("C:/test.txt", ("C:/%s.txt" % name))

        try:
            sleep(60.00)
        except:
            exit()

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

回答1:

Have you progressed from your original problem. 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.

Running pythonservice.exe with -debug from the command prompt was not a problem because it used correct PATH environment variable, but if your service is installed as a System service it's worth checking out if the System Path variable has all the paths for the required DLLs (MSVC, Python, System32). For me it was missing the python DLLs path, after that it worked again.