win32 Service: path to .py script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gregor Horvath

    #1

    win32 Service: path to .py script

    Hi,

    I have a testservice.py (see below). I installed the Windows-Service
    successfully. (via commandlineopti on install)
    The problem is that it runs only when it is in c:\windows\syst em32 or in
    the python path.
    I added the desired path (Y:\) to the PYTHONPATH environment variable
    for the system account and rebooted.
    When I start command line python and do a import testservice everything
    is fine.
    Starting the service with the script only in Y:\ gives the error in the
    event log:

    Python could not import the service's module

    exceptions.Impo rtError: No module named testservice

    Thanks for your help.

    --
    Greg

    testservice.py:

    import win32serviceuti l, win32service
    import servicemanager
    import win32api
    import win32event
    import sys

    class TestPipeService (win32serviceut il.ServiceFrame work):
    _svc_name_ = "MDE Dispatcher"
    _svc_display_na me_ = "MDE-Dispatcher"
    _svc_descriptio n_ = "Dispatches machine events from the Siemens ODC
    to registrered MDE clients (windows) over the network via XML-RPC"
    def __init__(self, args):
    win32serviceuti l.ServiceFramew ork.__init__(se lf, args)
    self.hWaitStop = win32event.Crea teEvent(None, 0, 0, None)

    def SvcStop(self):
    self.ReportServ iceStatus(win32 service.SERVICE _STOP_PENDING)
    win32event.SetE vent(self.hWait Stop)


    def SvcDoRun(self):
    # Write an event log record - in debug mode we will also
    # see this message printed.
    servicemanager. LogMsg(
    servicemanager. EVENTLOG_INFORM ATION_TYPE,
    servicemanager. PYS_SERVICE_STA RTED,
    (self._svc_name _, '')
    )

    while 1:
    win32api.Sleep( 2)
    rc = win32event.Wait ForSingleObject (self.hWaitStop , 2000)
    if rc == win32event.WAIT _OBJECT_0:
    break


    # Write another event log record.
    servicemanager. LogMsg(
    servicemanager. EVENTLOG_INFORM ATION_TYPE,
    servicemanager. PYS_SERVICE_STO PPED,
    (self._svc_name _, " TEST ")
    )


    if __name__=='__ma in__':
    win32serviceuti l.HandleCommand Line(TestPipeSe rvice)

  • Larry Bates

    #2
    Re: win32 Service: path to .py script

    Gregor Horvath wrote:
    Hi,
    >
    I have a testservice.py (see below). I installed the Windows-Service
    successfully. (via commandlineopti on install)
    The problem is that it runs only when it is in c:\windows\syst em32 or in
    the python path.
    I added the desired path (Y:\) to the PYTHONPATH environment variable
    for the system account and rebooted.
    When I start command line python and do a import testservice everything
    is fine.
    Starting the service with the script only in Y:\ gives the error in the
    event log:
    >
    Python could not import the service's module
    >
    exceptions.Impo rtError: No module named testservice
    >
    Thanks for your help.
    >
    --
    Greg
    >
    testservice.py:
    >
    import win32serviceuti l, win32service
    import servicemanager
    import win32api
    import win32event
    import sys
    >
    class TestPipeService (win32serviceut il.ServiceFrame work):
    _svc_name_ = "MDE Dispatcher"
    _svc_display_na me_ = "MDE-Dispatcher"
    _svc_descriptio n_ = "Dispatches machine events from the Siemens ODC
    to registrered MDE clients (windows) over the network via XML-RPC"
    def __init__(self, args):
    win32serviceuti l.ServiceFramew ork.__init__(se lf, args)
    self.hWaitStop = win32event.Crea teEvent(None, 0, 0, None)
    >
    def SvcStop(self):
    self.ReportServ iceStatus(win32 service.SERVICE _STOP_PENDING)
    win32event.SetE vent(self.hWait Stop)
    >
    >
    def SvcDoRun(self):
    # Write an event log record - in debug mode we will also
    # see this message printed.
    servicemanager. LogMsg(
    servicemanager. EVENTLOG_INFORM ATION_TYPE,
    servicemanager. PYS_SERVICE_STA RTED,
    (self._svc_name _, '')
    )
    >
    while 1:
    win32api.Sleep( 2)
    rc = win32event.Wait ForSingleObject (self.hWaitStop , 2000)
    if rc == win32event.WAIT _OBJECT_0:
    break
    >
    >
    # Write another event log record.
    servicemanager. LogMsg(
    servicemanager. EVENTLOG_INFORM ATION_TYPE,
    servicemanager. PYS_SERVICE_STO PPED,
    (self._svc_name _, " TEST ")
    )
    >
    >
    if __name__=='__ma in__':
    win32serviceuti l.HandleCommand Line(TestPipeSe rvice)
    >
    I believe that your problem is that services run under Local
    System account. Normally Local System account would not have
    a drive mapping Y:\. You can change the account that a service
    runs under in the Service Manager-Log On tab. Normally you
    wouldn't run services from mapped drives, they would be
    installed on local machine and started from there. If services
    need to access mapped drives, you need to put full UNC
    pathnames to access them or run the services under an account
    that has the drives mapped properly.

    -Larry Bates

    Comment

    • Gregor Horvath

      #3
      Re: win32 Service: path to .py script

      Larry Bates schrieb:
      I believe that your problem is that services run under Local
      System account. Normally Local System account would not have
      a drive mapping Y:\. You can change the account that a service
      You are absolutly correct.
      I moved the script to a local drive instead of a mapped network drive,
      now it works perfectly.

      Thank you a lot.

      --
      Servus, Gregor

      Comment

      Working...