Return to site

Python Win32serviceutil Install Service

broken image


You can check Windows system service status using Python.

Using psutil

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. Here are the examples of the python api win32serviceutil.QueryServiceStatus taken from open source projects. By voting up you can indicate which examples are most useful and appropriate. As is common with Python extensions, there are two Python modules that can work with services: The win32service module implements the Win32 service functions, while the win32serviceutil module provides some handy utilities that utilize the raw API.

>>> import win32serviceutil
>>> win32serviceutil.QueryServiceStatus('Service_Name')
(272, 4, 7, 0, 0, 0, 0) # 4 means it's running
>>> win32serviceutil.QueryServiceStatus('Service_Name')
(272, 1, 0, 0, 0, 0, 0) # 1 means it's stopped

Using wmi

You can lookup with conditions like StartMode and State. Following code enumerates services whose StartMode is 'Disabled' and current State is 'Stopped'.

>>> import wmi
>>> c=wmi.WMI()
>>> c.Win32_Service(StartMode='Disabled', State='Stopped')
[, , , , , , , , ]

Download ntlite alternative torrent. Check if specific service is running or not. Empty array is returned if the service is not running.

Install Win32serviceutil


>>> c.Win32_Service(Name='Netlogon', State='Running')
[]

I recently had to install a Python application as a Windows Service and lost a few hours to a seemingly obvious problem involving arguments.

Python Win32serviceutil Install Services

And of course I solved it rather quickly by using an old 'trick'; walking away and then coming back when I was refreshed.

I used the Python for Windows extensions, pywin32, library to create the service which inherited from win32serviceutil.ServiceFramework

The __init__ method receives two arguments; ‘self' and ‘args' but you can also always access sys.argv as normal.

Python Win32serviceutil Install Service
Python Win32serviceutil Install Service

But it turns out that sys.argv only contains the name of the PythonService, for example, [‘C:Python27libsite-packageswin32PythonService.exe'] which is used to actually call your main program. It does not contain any start parameters that you may have added to the Windows Service Manager when starting your service. But you can find these start parameters in the ‘args' parameter.

Win32serviceutil

The python application uses argparse.ArgumentParser from the argparse library. Transit rape tapegraffiti movies & documentaries. The application was failing with argparse complaining

File 'C:Python27libargparse.py', line 1937, in _parse_known_args
self.error(_('too few arguments'))

I tried every combination of args before I hit upon the answer:

The ‘args' parameter is a tuple!, Not a list as expected by a call to parser.parse_args().

Python Win32serviceutil Install Service Pack

Also sys.arvg has insufficient elements as it is received by __init__ Welcome  bookworms!thebookhub.

Python Win32serviceutil.serviceframework

So the solution is to change sys.argv to be the list that parser.parse_args() expects but using the elements in args.

sys.argv = [arg for arg in args]

Note:

This works when passing the arguments using the Windows Service Manager 'start parameters' field. But this value is not persisted (Why Microsoft, why?) so using the registry may be good place to store values needed for subsequent runs.

Service

But it turns out that sys.argv only contains the name of the PythonService, for example, [‘C:Python27libsite-packageswin32PythonService.exe'] which is used to actually call your main program. It does not contain any start parameters that you may have added to the Windows Service Manager when starting your service. But you can find these start parameters in the ‘args' parameter.

The python application uses argparse.ArgumentParser from the argparse library. Transit rape tapegraffiti movies & documentaries. The application was failing with argparse complaining

File 'C:Python27libargparse.py', line 1937, in _parse_known_args
self.error(_('too few arguments'))

I tried every combination of args before I hit upon the answer:

The ‘args' parameter is a tuple!, Not a list as expected by a call to parser.parse_args().

Python Win32serviceutil Install Service Pack

Also sys.arvg has insufficient elements as it is received by __init__ Welcome  bookworms!thebookhub.

Python Win32serviceutil.serviceframework

So the solution is to change sys.argv to be the list that parser.parse_args() expects but using the elements in args.

sys.argv = [arg for arg in args]

Note:

This works when passing the arguments using the Windows Service Manager 'start parameters' field. But this value is not persisted (Why Microsoft, why?) so using the registry may be good place to store values needed for subsequent runs.

Another workaround is to edit the registry value for your service.

In the Registry Editor window, navigate to HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServices and open the key ImagePath.

After the path to the PythonService you may add arguments.

Now you will NOT need to mess with sys.argv and it can be left as is since it now has the arguments in a list as expected.





broken image