OmegaNine
OmegaNine

Reputation: 371

PIP installed pywin32 service fails: Incorrect function

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!

Upvotes: 1

Views: 2211

Answers (3)

Aelius
Aelius

Reputation: 1381

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.

Upvotes: 0

Czechnology
Czechnology

Reputation: 14992

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.

Upvotes: 0

OmegaNine
OmegaNine

Reputation: 371

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.

Upvotes: 1

Related Questions