-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDSMPi_service
executable file
·117 lines (94 loc) · 2.54 KB
/
DSMPi_service
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python3
# -*- coding:utf-8 -*-
import os
import sys
import time
import argparse
from mydaemon import Daemon
DAEMON_NAME = 'DSMPi'
DAEMON_STOP_TIMEOUT = 10
PIDFILE = '/run/DSMPi.pid'
RUNFILE = '/run/DSMPi.run'
LOOP_SLEEP = 2
DEBUG = 0
def get_args():
'''
>>> get_args()
('start',)
>>> get_args()
('stop',)
'''
try:
parser = argparse.ArgumentParser()
parser.add_argument('action', help='action',
choices=['start', 'stop', 'restart'])
args = parser.parse_args()
result = (args.action, )
except Exception as err:
if DEBUG:
raise
else:
sys.stderr.write('%s\n' % (err))
result = (None, )
return result
# -----------------------------------------------------------------------------
def wait(timeout=60):
'''
>>> wait(20)
True
>>> wait(None)
False
'''
try:
# Wait [timeout] seconds while there is no stop request.
t0 = time.time()
while os.path.exists(RUNFILE) and ((time.time()-t0) < timeout):
time.sleep(1.0)
result = True
except Exception as err:
if DEBUG:
raise
else:
sys.stderr.write('%s\n' % (err))
result = False
return result
def kickit():
subprocess.run(["/opt/github/DSMPi/bg_temp-to-screen start 5455"], shell = True)
class MyDaemon(Daemon):
def run(self):
kickit()
# Run while there is no stop request.
while os.path.exists(RUNFILE):
try:
pass
except Exception as err:
if DEBUG:
raise
else:
sys.stderr.write('%s\n' % (err))
finally:
wait(timeout=LOOP_SLEEP)
# -----------------------------------------------------------------------------
if __name__ == '__main__':
try:
# Get arguments.
(action, ) = get_args()
# Create daemon object.
d = MyDaemon(name=DAEMON_NAME, pidfile=PIDFILE, runfile=RUNFILE,
stoptimeout=DAEMON_STOP_TIMEOUT, debug=DEBUG)
# Action requested.
if action == 'start':
d.start()
elif action == 'stop':
d.stop()
elif action == 'restart':
d.restart()
else:
raise NameError('Unknown action')
sys.exit(0)
except Exception as err:
if DEBUG:
raise
else:
sys.stderr.write('%s\n' % err)
sys.exit(1)