Python Win32serviceutil Install Service

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.

service.py
importwin32serviceutil
importwin32service
importwin32event
importservicemanager
# import signal
# import os
frommultiprocessingimportProcess
frommainimportapp
# Usage:
# python service.py install
# python service.py start
# python service.py stop
# python service.py remove
# python service.py --username <username> --password <password> --startup auto install
classService(win32serviceutil.ServiceFramework):
_svc_name_='ServiceName'
_svc_display_name_='Service Display Name'
_svc_description_='Service description.'
def__init__(self, *args):
super().__init__(*args)
defSvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.process.terminate()
# os.kill(os.getpid(), signal.SIGTERM)
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
defSvcDoRun(self):
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ')
)
self.process=Process(target=self.main)
self.process.start()
self.process.run()
defmain(self):
pass
if__name__'__main__':
win32serviceutil.HandleCommandLine(Service)

commented Feb 2, 2017

In which circumstance are --username and --password parameters used? Are they required only for installing the service as automatic starting?
Could you please give an example of the username including the domain name?

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
8 Aug 2016CPOL

Introduction

This is about the way how to make a Windows service using Python.

Background

For business thing, I have to transfer data from SQLServer to MongoDB. And it needs to occur everyday. So I have to make it a Windows service.

Using the Code

This is a packaging code, for making my logic which is written with Python to a Windows service.

The code is like this:

Of course, we do not want to install Python environment on our production server. So I have to make it an executable file, which is EXE for Windows. The problem is that the version of python I used is 3.5, so the famous tool pywin32 is not available for me.(As one of the comments says, pywin32 is available now, I wrote this long time ago, sorry for not checking it out.) After searching, finally I found it--pyinstaller.

Download and install it. Then run cmd, typing the code like below:

There are a lot of other parameters, and we can get help from http://www.pyinstaller.org/.

About --hidden-import, now it has an alias called --hiddenimport.

Download Casio SE-S400.

When I run pyinstaller --onefile DataTransToMongoService.py, I get a error which shows ImportError: No module named win32timezone.

Then I add that option. But now, pyinstaller has solved this problem. I don't need --hiddenimport anymore.

2016/08/09

Last night I researched the code, and found that there is something can be optimized.

The service can not be closed normally. So I write a sample for test, and it goes well.

The sample code is like below:

History

Python
  • 1st August, 2016: Initial version
  • 9th August, 2016: fix the unclosable bug.( unclosable => can not be closed ^^)