-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyservice.py
38 lines (31 loc) · 1.04 KB
/
myservice.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
import logging
from logging.handlers import SysLogHandler
import time
from service import find_syslog, Service
class MyService(Service):
def __init__(self, *args, **kwargs):
super(MyService, self).__init__(*args, **kwargs)
self.logger.addHandler(SysLogHandler(address=find_syslog(),
facility=SysLogHandler.LOG_DAEMON))
self.logger.setLevel(logging.INFO)
def run(self):
while not self.got_sigterm():
self.logger.info("I'm working...")
time.sleep(5)
if __name__ == '__main__':
import sys
if len(sys.argv) != 2:
sys.exit('Syntax: %s COMMAND' % sys.argv[0])
cmd = sys.argv[1].lower()
service = MyService('my_service', pid_dir='/tmp')
if cmd == 'start':
service.start()
elif cmd == 'stop':
service.stop()
elif cmd == 'status':
if service.is_running():
print("Service is running.")
else:
print("Service is not running.")
else:
sys.exit('Unknown command "%s".' % cmd)