Skip to content

Instantly share code, notes, and snippets.

@behitek
Created December 23, 2019 06:37
Show Gist options
  • Save behitek/c12d6deebed229e1139e61742c2ccb90 to your computer and use it in GitHub Desktop.
Save behitek/c12d6deebed229e1139e61742c2ccb90 to your computer and use it in GitHub Desktop.
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import time
#1. Sửa hàm main, chèn code của anh vào chỗ Your code here
#2. Chạy code này ở command line với tham số install
#Link: https://stackoverflow.com/questions/32404/
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "TestService"
_svc_display_name_ = "Test Service"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self.main()
def main(self):
# Your code here
while True:
print("TestService is running...")
time.sleep(5)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment