Created
December 23, 2019 06:37
-
-
Save behitek/c12d6deebed229e1139e61742c2ccb90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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