-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
77 lines (68 loc) · 2.12 KB
/
main.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
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
# -*- coding:utf-8 -*-
'''
# Created on 2013年10月17日
#
# @author: Echo
# '''
import signal
import sys
import os
from edgemanager.edge_manager import *
from config.config import *
from config.globals import *
import cPickle as pickle
def main():
#初始化时从文件读入设备状态表
try:
filename = PROGRAM_ROOT + os.sep + 'temps' + os.sep + 'devicemap'
f = open(filename, 'r')
globals.deviceMap = pickle.load(f)
logger.info(globals.deviceMap)
except:
pass
edgemanager = EdgeManager(SERVER_PORT, CMSG_PORT)
edgemanager.run()
app.run()
def exit_handler(signal, frame):
#捕获SIGTERM信号,将设备状态表存入磁盘配置文件
filename = PROGRAM_ROOT + os.sep + 'temps' + os.sep + 'devicemap'
f = open(filename, 'w')
pickle.dump(globals.deviceMap, f)
#随后无条件杀死
f = open(PROGRAM_ROOT + os.sep + "sys.pid", "r")
pid = int(f.readline())
cmd = "kill -9 %d" % pid
os.system(cmd)
def daemonize(stdin = '/dev/null', stdout = '/dev/null', stderr = '/dev/null'):
try:
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errorno, e.strerror))
sys.exit(1)
os.chdir('/')
os.umask(0)
os.setsid()
try:
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errorno, e.strerror))
sys.exit(1)
for f in sys.stdout, sys.stderr:
f.flush()
si = file(stdin, 'r')
so = file(stdout, 'a+')
se = file(stderr, 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
if __name__ == "__main__":
signal.signal(signal.SIGTERM, exit_handler)
daemonize(stdout = PROGRAM_ROOT + os.sep + 'sys.log', stderr = PROGRAM_ROOT + os.sep + 'sys.err')
pidfile = open(PROGRAM_ROOT + os.sep + "sys.pid", "w")
pidfile.write(str(os.getpid()))
pidfile.close()
main()