-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_world_service.py
43 lines (37 loc) · 1.32 KB
/
hello_world_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import sys
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import time
class HelloWorldService(win32serviceutil.ServiceFramework):
_svc_name_ = 'HelloWorldService'
_svc_display_name_ = 'Hello World Service'
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
self.is_alive = True
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.is_alive = False
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.main()
def main(self):
while self.is_alive:
with open('hello_world.txt', 'a') as f:
f.write('Hello World\n')
time.sleep(60)
if __name__ == '__main__':
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(HelloWorldService)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(HelloWorldService)