Advertisement
Guest User

Run Python scripts as a service example

a guest
Oct 31st, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. ### Run Python scripts as a service example (ryrobes.com)
  2. ### Usage : python aservice.py install (or / then start, stop, remove)
  3.  
  4. import win32service
  5. import win32serviceutil
  6. import win32api
  7. import win32con
  8. import win32event
  9. import win32evtlogutil
  10. import os, sys, string, time
  11.  
  12. class aservice(win32serviceutil.ServiceFramework):
  13.    
  14.    _svc_name_ = "MyServiceShortName"
  15.    _svc_display_name_ = "My Serivce Long Fancy Name!"
  16.    _svc_description_ = "THis is what my crazy little service does - aka a DESCRIPTION! WHoa!"
  17.          
  18.    def __init__(self, args):
  19.            win32serviceutil.ServiceFramework.__init__(self, args)
  20.            self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)          
  21.  
  22.    def SvcStop(self):
  23.            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  24.            win32event.SetEvent(self.hWaitStop)                    
  25.          
  26.    def SvcDoRun(self):
  27.       import servicemanager      
  28.       servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))
  29.      
  30.       #self.timeout = 640000    #640 seconds / 10 minutes (value is in milliseconds)
  31.       self.timeout = 120000     #120 seconds / 2 minutes
  32.       # This is how long the service will wait to run / refresh itself (see script below)
  33.  
  34.       while 1:
  35.          # Wait for service stop signal, if I timeout, loop again
  36.          rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
  37.          # Check to see if self.hWaitStop happened
  38.          if rc == win32event.WAIT_OBJECT_0:
  39.             # Stop signal encountered
  40.             servicemanager.LogInfoMsg("SomeShortNameVersion - STOPPED!")  #For Event Log
  41.             break
  42.          else:
  43.  
  44.                  #Ok, here's the real money shot right here.
  45.                  #[actual service code between rests]
  46.                  try:
  47.                      file_path = "C:\Services\Mylar\Mylar.py"
  48.                      execfile(file_path)             #Execute the script
  49.  
  50.                     # inc_file_path2 = "C:\whereever\MORE_REAL_py_work_to_be_done.py"
  51.                     # execfile(inc_file_path2)        #Execute the script
  52.                  except:
  53.                      pass
  54.                  #[actual service code between rests]
  55.  
  56.  
  57. def ctrlHandler(ctrlType):
  58.    return True
  59.                  
  60. if __name__ == '__main__':  
  61.    win32api.SetConsoleCtrlHandler(ctrlHandler, True)  
  62.    win32serviceutil.HandleCommandLine(aservice)
  63.  
  64. # Done! Lets go out and get some dinner, bitches!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement