Sei sulla pagina 1di 3

http://ryrobes.

com/python/running-python-scripts-as-a-windows-service/
http://code.activestate.com/recipes/576451-how-to-create-a-windows-service-in-py
thon/
==========================================================================
### Run Python scripts as a service example (ryrobes.com)
### Usage : python aservice.py install (or / then start, stop, remove)
import
import
import
import
import
import
import

win32service
win32serviceutil
win32api
win32con
win32event
win32evtlogutil
os, sys, string, time

class aservice(win32serviceutil.ServiceFramework):
_svc_name_ = "MyServiceShortName"
_svc_display_name_ = "My Serivce Long Fancy Name!"
_svc_description_ = "THis is what my crazy little service does - aka a DESCRI
PTION! WHoa!"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
import servicemanager
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemana
ger.PYS_SERVICE_STARTED,(self._svc_name_, ''))
#self.timeout = 640000

#640 seconds / 10 minutes (value is in milliseco

nds)
self.timeout = 120000
#120 seconds / 2 minutes
# This is how long the service will wait to run / refresh itself (see scri
pt below)
while 1:
# Wait for service stop signal, if I timeout, loop again
rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
# Check to see if self.hWaitStop happened
if rc == win32event.WAIT_OBJECT_0:
# Stop signal encountered
servicemanager.LogInfoMsg("SomeShortNameVersion - STOPPED!") #For E
vent Log
break
else:
#Ok, here's the real money shot right here.
#[actual service code between rests]
try:
file_path = "C:\whereever\my_REAL_py_work_to_be_done.py"
execfile(file_path)
#Execute the script

inc_file_path2 = "C:\whereever\MORE_REAL_py_work_to_be_done
.py"
execfile(inc_file_path2)
#Execute the script
except:
pass
#[actual service code between rests]
def ctrlHandler(ctrlType):
return True
if __name__ == '__main__':
win32api.SetConsoleCtrlHandler(ctrlHandler, True)
win32serviceutil.HandleCommandLine(aservice)
# Done! Lets go out and get some dinner, bitches!
=============================================================================
'''
Author: Alex Baker
Date: 7th July 2008
Description : Simple python program to generate wrap as a service based on exam
ple on the web, see link below.
http://essiene.blogspot.com/2005/04/python-windows-services.html
Usage
Usage
Usage
Usage

:
:
:
:

python
python
python
python

aservice.py
aservice.py
aservice.py
aservice.py

install
start
stop
remove

C:\>python aservice.py --username <username> --password <PASSWORD> --startup a


uto install
'''
import
import
import
import
import
import
import

win32service
win32serviceutil
win32api
win32con
win32event
win32evtlogutil
os

class aservice(win32serviceutil.ServiceFramework):
_svc_name_ = "aservice"
_svc_display_name_ = "a service - it does nothing"
_svc_description_ = "Tests Python service framework by receiving and echoing
messages over a named pipe"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)

win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
import servicemanager
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemana
ger.PYS_SERVICE_STARTED,(self._svc_name_, ''))
self.timeout = 3000
while 1:
# Wait for service stop signal, if I timeout, loop again
rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
# Check to see if self.hWaitStop happened
if rc == win32event.WAIT_OBJECT_0:
# Stop signal encountered
servicemanager.LogInfoMsg("aservice - STOPPED")
break
else:
servicemanager.LogInfoMsg("aservice - is alive and well")
def ctrlHandler(ctrlType):
return True
if __name__ == '__main__':
win32api.SetConsoleCtrlHandler(ctrlHandler, True)
win32serviceutil.HandleCommandLine(aservice)

Potrebbero piacerti anche