[python-win32] FW: Python script as a windows service

Jim Vickroy Jim.Vickroy at noaa.gov
Thu Nov 2 17:39:46 CET 2006


I am not familiar with this approach (and I am working on a machine 
where I do not have admin privileges), but an alternative is to download 
the Windows extensions for Python by Mark Hammond (et. al.) at:

http://sourceforge.net/projects/pywin32/

which makes writing a Windows service simple as the following example 
illustrates:

=====================================================
# SmallestService.py
#
# A sample demonstrating the smallest possible service written in Python.

import win32serviceutil
import win32service
import win32event

class SmallestPythonService(win32serviceutil.ServiceFramework):
    _svc_name_ = "SmallestPythonService"
    _svc_display_name_ = "The smallest possible Python Service"
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        # Create an event which we will use to wait on.
        # The "service stop" request will set this event.
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        # Before we do anything, tell the SCM we are starting the stop 
process.
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        # And set my event.
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        # We do nothing other than wait to be stopped!
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

if __name__=='__main__':
    win32serviceutil.HandleCommandLine(SmallestPythonService)
=====================================================

Just type:

SmallestService.py -h

at a shell prompt to see the options for installing/removing etc.

-- jv



Tor Erik Sønvisen wrote:

> Hi,
>  
> I'm trying to execute a python script as a windows service.
> I found some information at:
> http://agiletesting.blogspot.com/2005/09/running-python-script-as-windows.html
>  
> The steps I've taken are the following:
>  
> 1. Downloaded Windows Resource Kits
> 2. Executed instsrv.exe nameOfService srvany.exe
> 3. In registry added a key Parameters in 
> HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\nameOfService entry
> 4. Under Parameters added three string values:
>     4.1: Application                REG_SZ                    
> C:\Python25\pythonw.exe
>     4.2: AppDirectory            REG_SZ                    C:\Python25
>     4.3: AppParameters         REG_SZ                    C:\Documents 
> and Settings\torerik\My Documents\Apps\SARImageProcessing\time.py
> 5. Started nameOfService in Control Panel --> Adminstrative Tools --> 
> Services
>  
> This is not working however as the python script (time.py) looks like 
> this:
>  
> import time
>  
> i = 0
> while True:
>     f = open(r'C:\testService.txt', 'wb')
>     f.write(str(i))
>     f.close()
>     i += 1
>     time.sleep(1)
>  
> and no file named testService.txt is created.
> I've tried executing the script normally, and it works like a charm...
> Any ideas?
>  
> regards
>  
>  
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Python-win32 mailing list
>Python-win32 at python.org
>http://mail.python.org/mailman/listinfo/python-win32
>  
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20061102/68b2238c/attachment.html 


More information about the Python-win32 mailing list