Python Win32serviceutil Install Service

Posted on  by 

drec4s :

Nssm is an excellent tool to install your services with, unlike sc or from raw python code with win32serviceutil. Have a module that calls the main function and run the module inside a batch file. Then the batch file can be used for the windows service. In the python module: ' main.py ' from myCode import run.py run In service.bat. The functions this recipe uses from the win32serviceutil module are StartService, StopService, RestartService, and QueryServiceStatus. Each takes two arguments: the name of the service and the name of the machine. The first three perform the start, stop, and restart as requested. The fourth returns a structured code describing whether and how. Nssm is an excellent tool to install your services with, unlike sc or from raw python code with win32serviceutil. Have a module that calls the main function and run the module inside a batch file. Then the batch file can be used for the windows service. In the python module: ' main.py ' from myCode import run.py run In service.bat. I just create a simple 'how to' where the program is in one module and the service is in another place, it uses py2exe to create the win32 service, which I believe is the best you can do for your users that don't want to mess with the python interpreter or other dependencies. For Python you can do this, which creates the service in one go: nssm install MyServiceName c: python27 python.exe c: temp myscript.py. Where myscript.py is the boilerplate script above, modified to invoke your application script or code in the main function. Note that the service doesn't run the Python script directly, it runs the Python.

I am trying to get a Flask app to run as a Service in Windows. I have already tried to implement a solution as suggested here and here without success.

I have a simple folder with just two files:

Python Win32serviceutil Install Service Panel

Inside myapp.py is a simple Flask app:

And the service skeleton win32_service.py:

I then compiled this to an exe file via pyinstaller using this command:

I get the compiled exe successfully built. I then proceed to register the service (open cmd with admin privileges):

And I try to start it:

But then nothing happens (no errors). Also if I try to start it from the Task Manager it changes the Status to Starting and then to Stopped.

These are the modules installed in the virtualenv:

System specs:

What I am missing here? Any help is appreciated.

EDIT

Windows EventViewer shows an error:

EDIT 2

If I use a single spec file, some modules are not found by the hidden import (this is the output from pyinstaller:

Could it have to do with this? Why are some modules found and others don't? I am using a virtualenv.

drec4s :

I looked further into pyinstaller github repo and solved this issue.

It seems that pyinstaller has some conflicts with Windows 10, but this issue was the key to my problem. Althoug the module producing the error was not the same.

I managed to solve it by adding a SystemError exception at libsite-packagesclickutils.py, line 260 in the echo function.

Installutil

So I change this:

To this:

Rebuilt the exe using:

Service

Installed the service, and then it started correctly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

Login to comment

Related

Python Win32serviceutil Install Service

HotTag

Coments are closed