1

Running Python 3.6.5, PyWin32 223.
After install I have included the the C:\Python32\lib\win32 folder in path.

I am just running the normal testService shell that seems to be all over the internet. Can be found below.

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket


class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "TestService"
    _svc_display_name_ = "Test Service"

    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):
        pass

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

I can "compile" and install the service fine, as soon as I run it I get an error to check the system logs. When I do I get the following error.

The Test Service Process service terminated with the service-specific error Incorrect function..

I have not been able to find any help through google from this decade. I am new to the library and the only help I found was to add its lib to the path. If its a path error, its no longer that. Anyone have any ideas?

Thanks in advance!

OmegaNine
  • 371
  • 1
  • 4
  • 15
  • I can't get your error. Here's an example: https://www.codeproject.com/Articles/1115336/Using-Python-to-Make-a-Windows-Service. – CristiFati Jul 10 '18 at 07:09

3 Answers3

1

It turns out that it was a permission issue. I request root for 30 seconds and worked like a charm. Just FYI if anyone is having this problem.

OmegaNine
  • 371
  • 1
  • 4
  • 15
0

In my case the problem was in the way I run the python module. Instead of executing the Python script, I use

$ python -m module

This works correctly when running in the appropriate directory but when running as a service, the module could not be found. So the solution if executing modules directly with Python is to pip install the module so that it can be found by the service.

Czechnology
  • 14,832
  • 10
  • 62
  • 88
0

In my case was due to the fact that the python script I was trying to run as a service wasn't in the same drive of the python installation. Also check this useful answer.

Aelius
  • 1,029
  • 11
  • 22