Implementing MS Windows Services in Python |
IntroductionThis document describes in short terms how to implement a Windows service that is capable of running on most Microsoft-based operating system. The reason for publishing this rather short document is the lack of a proper example describing how to implement a simple service and while the content below functions as expected on my systems I'm not an expert in the area, but I hope this will be serve as a quick-start for other developers in the need of a native Windows service implemented in their favorite programming language, Python. The following content assumes direct knowledge of what a service is and how to program in the Python programming language.Table of Contents0. py2exeWindows isn't capable of natively running Python scripts directly and as such it requires some intermediary functionality to make this happen - what this means in terms of creating services is that we need to create an exe-file that conforms to the definitions of how a service should function and should be interacted with. Thankfully most of this is done by modules included with the standard Python package, but when it comes to converting a script to an exe-file we'll need to download and install the py2exe-module from it's website at http://www.py2exe.org/.With py2exe installed a service can be created and built using python by implementing certain parts of the installation specification within a separate file that takes care of describing how the application should be built and packaged. The corresponding file to the example used in the later example should be called "setup.py", and it should have the following contents:
Most of this is straight forward and as you can see some information can be embedded into the exe-file itself, but for now the most interesting part is that we specify that a module called Service should be built (implementation expected to be found in a file called Service.py). Some standard libraries are specifically referenced to make sure that they're included, but mostly py2exe figures out what is needed by itself.
The application itself can be built by issuing the following command (note that this will fail as we haven't actually created the module yet): python setup.py py2exe 0. DemoServiceWith the py2exe in place as well as the setup scripts for building the application it's time to actually add the functionality that will become our service. A very simple service implementation, place in a file called "Service.py", can look like the following:
Most of this is code that can be used as a template and apart from the rather simple contents of the SvcDoRun-function where we actually implement our service the won't be much difference between the more simpler services. Basically what we do here is provide a basic implementation of how the service should perform standards tasks such as starting and stopping itself and while doing that inform the operating system about it's status.
The actual service as implemented here simply uses a timeout-function set to expire every second in a loop, and at each iteration we write and informational message to the Application Event Log. Please note that services shouldn't print data to the console using print as this is done to a limited buffer that is never flushed - the eventual result being a full buffer and a service that appears to hang without any apparent reason. The service can now be built as described in the previous chapter and installed on the system as a service. You'll probably soon notice the existence of a new exe-file in the dist-subdirectory named Service.exe (after the module name), and this executable can be used to install the service on the system in the following way (note that this must be done using administrator privileges): The service should now be registered on the system and can be started via the standard interface, often via the Microsoft Management Console configuration "services.msc" - note the system events logged to the windows event log. After testing the service you'll probably want to remove it in order to stop it from cluttering up your system, and this can be done in the following manner:Service.exe -install Service.exe -remove |