Locked History Actions

attachment:servicerunner.py of HelpOnInstalling/TwistedWeb

Attachment 'servicerunner.py'

Download

   1 #! /usr/bin/env python
   2 #coding=utf-8
   3 
   4 import win32serviceutil
   5 import win32service
   6 import subprocess
   7 
   8 #daemon process command line
   9 # this is modified from mointwisted.cmd
  10 mointwisted = r'D:\Python26\python.exe "D:\Python26\Scripts\twistd.py" --python mointwisted.py'
  11 #daemon process current dir
  12 mointwisted_dir = r"E:\Moin\moin"
  13 
  14 
  15 #py2.5 use TerminateProcess to kill service daemon
  16 import ctypes
  17 
  18 kernel32 = ctypes.windll.LoadLibrary('kernel32.dll')
  19 
  20 def kill_process(pid):
  21     handle = kernel32.OpenProcess(1, False,pid)
  22     if handle:
  23         kernel32.TerminateProcess(handle,0)
  24     else:
  25         print 'can\'t open process %s' % pid
  26 
  27 class servicerunner(win32serviceutil.ServiceFramework):
  28     _svc_name_ = "mointwisted"
  29     _svc_display_name_ = "mointwisted"
  30     def __init__(self, args):
  31         win32serviceutil.ServiceFramework.__init__(self, args)
  32 
  33         #init stop flag
  34         self.service_want_to_stop = False
  35 
  36     def SvcStop(self):
  37         # tell SCM I am stoping
  38         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  39 
  40         #set stop flag
  41         self.service_want_to_stop = True
  42 
  43         #py2.6 use Popen.terminate() to kill service daemon process
  44         #self.p.terminate()
  45 
  46         #py2.5 use TerminateProcess() to kill service daemon process
  47         kill_process(self.p.pid)
  48 
  49     def SvcDoRun(self):
  50         #now I do not want to stop
  51         self.service_want_to_stop = False
  52 
  53         #this "while" is to make sure the daemon auto restarting when
  54         #ending with a error or crashing,when I do not want to stop
  55         while (self.service_want_to_stop == False):
  56             self.p = subprocess.Popen(mointwisted,cwd=mointwisted_dir)
  57             self.p.wait()
  58 
  59 if __name__=='__main__':
  60     '''usage:

  61     u can use 'servicerunner.py install' to install service.

  62     'servicerunner.py will give the usage' '''
  63     win32serviceutil.HandleCommandLine(servicerunner)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2011-02-21 07:14:24, 2.0 KB) [[attachment:servicerunner.py]]

You are not allowed to attach a file to this page.