Skip to content

Instantly share code, notes, and snippets.

@DoctorMalboro
Created July 17, 2014 18:31
Show Gist options
  • Save DoctorMalboro/dcee91d732a688a1fcfc to your computer and use it in GitHub Desktop.
Save DoctorMalboro/dcee91d732a688a1fcfc to your computer and use it in GitHub Desktop.
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "ShortAndTogetherName"
_svc_display_name_ = "Full name of your service"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args) # Creator of the service object
self.hWaitStop = win32event.CreateEvent(None,0,0,None) # Creates the event
socket.setdefaulttimeout(60) # By default, all services should have a timeout in case the service doesn't stop or start
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) # Sends the signal to the service window that the service is stopping
win32event.SetEvent(self.hWaitStop) # Sends the signal to actually stop the service
self.stop() # I use a function to stop a webapp, but you can actually put code in here to do whatever you need to do. It doesn't always stop or kill the process, so don't trust on not using the stop() function.
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,'')) # Sends a log with the event of our service starting
self.main() # The function I use to start the service
def main(self):
# Stuff goes here...
def stop(self):
# Stuff goes here...
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc) # Executes all the commands to install, run, stop, restart and delete the service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment