Python Win32serviceutil Install Service

Dec 06, 2020 This is pretty simple – if there is a single argument (the script/binary itself), then start the service control dispatcher to allow Windows to manage the service. If there are any other arguments, assume that it is arguments to manager the service, e.g. Install, or start. In both cases, we send our ServiceFramework subclass as a parameter. The following are 6 code examples for showing how to use win32service.StartService.These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

  1. Python Win32serviceutil Install Service Tool
  2. Python Win32serviceutil Install Service Pack
  3. Python Win32serviceutil Install Service Pack
  4. Python Win32serviceutil Install Service Tool
Python Win32serviceutil Install Service

Question or problem about Python programming:

I am sketching the architecture for a set of programs that share various interrelated objects stored in a database. I want one of the programs to act as a service which provides a higher level interface for operations on these objects, and the other programs to access the objects through that service.

I am currently aiming for Python and the Django framework as the technologies to implement that service with. I’m pretty sure I figure how to daemonize the Python program in Linux. However, it is an optional spec item that the system should support Windows. I have little experience with Windows programming and no experience at all with Windows services.

Is it possible to run a Python programs as a Windows service (i. e. run it automatically without user login)? I won’t necessarily have to implement this part, but I need a rough idea how it would be done in order to decide whether to design along these lines.

Edit: Thanks for all the answers so far, they are quite comprehensive. I would like to know one more thing: How is Windows aware of my service? Can I manage it with the native Windows utilities? What is the equivalent of putting a start/stop script in /etc/init.d?

How to solve the problem:

Solution 1:

Python Win32serviceutil Install Service Tool

Yes you can. I do it using the pythoncom libraries that come included with ActivePython or can be installed with pywin32 (Python for Windows extensions).

This is a basic skeleton for a simple service:

Your code would go in the main() method—usually with some kind of infinite loop that might be interrupted by checking a flag, which you set in the SvcStop method

Solution 2:

Although I upvoted the chosen answer a couple of weeks back, in the meantime I struggled a lot more with this topic. It feels like having a special Python installation and using special modules to run a script as a service is simply the wrong way. What about portability and such?

I stumbled across the wonderful Non-sucking Service Manager, which made it really simple and sane to deal with Windows Services. I figured since I could pass options to an installed service, I could just as well select my Python executable and pass my script as an option.

I have not yet tried this solution, but I will do so right now and update this post along the process. I am also interested in using virtualenvs on Windows, so I might come up with a tutorial sooner or later and link to it here.

Solution 3:

The simplest way is to use the: NSSM – the Non-Sucking Service Manager. Just download and unzip to a location of your choosing. It’s a self-contained utility, around 300KB (much less than installing the entire pywin32 suite just for this purpose) and no “installation” is needed. The zip contains a 64-bit and a 32-bit version of the utility. Either should work well on current systems (you can use the 32-bit version to manage services on 64-bit systems).

GUI approach

1 – install the python program as a service. Open a Win prompt as admin

Python Win32serviceutil Install Service

2 – On NSSM´s GUI console:

path: C:Python27Python27.exe

Startup directory: C:Python27

Arguments: c:WinService.py

3 – check the created services on services.msc

Scripting approach (no GUI)

This is handy if your service should be part of an automated, non-interactive procedure, that may be beyond your control, such as a batch or installer script. It is assumed that the commands are executed with administrative privileges.

For convenience the commands are described here by simply referring to the utility as nssm.exe. It is advisable, however, to refer to it more explicitly in scripting with its full path c:pathtonssm.exe, since it’s a self-contained executable that may be located in a private path that the system is not aware of.

1. Install the service

You must specify a name for the service, the path to the proper Python executable, and the path to the script:

More explicitly:

Alternatively you may want your Python app to be started as a Python module. One easy approach is to tell nssm that it needs to change to the proper starting directory, as you would do yourself when launching from a command shell:

This approach works well with virtual environments and self-contained (embedded) Python installs. Just make sure to have properly resolved any path issues in those environments with the usual methods. nssm has a way to set environment variables (e.g. PYTHONPATH) if needed, and can also launch batch scripts.

2. To start the service

Python Win32serviceutil Install Service

3. To stop the service

4. To remove the service, specify the confirm parameter to skip the interactive confirmation.

Solution 4:

There are a couple alternatives for installing as a service virtually any Windows executable.

Python

Python Win32serviceutil Install Service Pack

Method 1: Use instsrv and srvany from rktools.exe

For Windows Home Server or Windows Server 2003 (works with WinXP too), the Windows Server 2003 Resource Kit Tools comes with utilities that can be used in tandem for this, called instsrv.exe and srvany.exe. See this Microsoft KB article KB137890 for details on how to use these utils.

For Windows Home Server, there is a great user friendly wrapper for these utilities named aptly “Any Service Installer“.

Python Win32serviceutil Install Service Pack

Method 2: Use ServiceInstaller for Windows NT

There is another alternative using ServiceInstaller for Windows NT (download-able here) with python instructions available. Contrary to the name, it works with both Windows 2000 and Windows XP as well. Here are some instructions for how to install a python script as a service.


Installing a Python script
Run ServiceInstaller to create a new
service. (In this example, it is
assumed that python is installed at
c:python25)
Service Name : PythonTest
Display Name : PythonTest
Startup : Manual (or whatever you like)
Dependencies : (Leave blank or fill to fit your needs)
Executable : c:python25python.exe
Arguments : c:path_to_your_python_scripttest.py
Working Directory : c:path_to_your_python_script

After installing, open the Control
Panel’s Services applet, select and
start the PythonTest service.

After my initial answer, I noticed there were closely related Q&A already posted on SO. See also:

Solution 5:

The simplest way to achieve this is to use native command sc.exe:

References:

Hope this helps!

There are several ways to create and install a Python application as a Service in Windows. Learn More about Windows service using Python here.

Headless processes (with no UI) in Windows are called Services. They can be controlled (started, stopped, etc) using standard Windows controls such as the command console, PowerShell or the Services tab in Task Manager. A good example might be an application that provides network services, such as a web application, or maybe a backup application that performs various background archival tasks.

Windows service using Python: A Python script that can be run as a service

The modules used in this example are part of pywin32 (Python for Windows extensions). Depending on how you installed Python, you might need to install this separately.

This is just boilerplate. Your application code, probably invoking a separate script, would go in the main() function.

Python Win32serviceutil Install Service Tool

You will also need to install this as a service. The best solution for this at the moment appears to be to use Non-sucking Service Manager. This allows you to install a service and provides a GUI for configuring the command line the service executes. For Python you can do this, which creates the service in one go:

Where my_script.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 interpreter and passes it the

main script on the command line.

Alternatively you can use tools provided in the Windows Server Resource Kit for your operating system version so create the service.

Running a Flask web application as a service

This is a variation on the generic example. You just need to import your app script and invoke it’s run() method in the service’s main() function. In this case we’re also using the multiprocessing module due to an issue accessing WSGIRequestHandler.

Adapted from https://stackoverflow.com/a/25130524/318488