-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_confd.py
More file actions
59 lines (52 loc) · 2.01 KB
/
Copy pathauto_confd.py
File metadata and controls
59 lines (52 loc) · 2.01 KB
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
#!/usr/bin/env python
# coding: utf-8
import os, sys, time, commands, logging
def daemonize():
logging.basicConfig(filename="./auto_conf.log", level=logging.DEBUG)
try:
# 程序分成两个不同的进程 同时执行下面的代码
# 在子进程内 fork方法会返回0
# 在父进程内 fork方法会返回子进程的编号PID
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError, e:
logging.debug("1st fork failed: %d %s\n" % (e.errno, e.strerror))
sys.exit(1)
# os.chdir("/")
os.umask(0)
os.setsid() # ??????
# 第二次fork
try:
pid = os.fork()
if pid > 0:
# logging.debug("pid: %d" % pid)
f = open('pid', 'w')
f.write("%d" % pid)
f.close()
sys.exit()
except OSError, e:
logging.debug("2nd fork failed: %d %s\n" % (e.errno, e.strerror))
sys.exit(1)
def main():
from time import sleep
try:
sys.stdout.write("[%s]:auto config start" % (time.strftime('%Y-%m-%d %H%M%S', time.localtime(time.time()))))
commands.getstatusoutput("sudo bash rebuild.sh")
nginx_time = os.stat("nginx.json").st_mtime
rinetd_time = os.stat("rinetd.json").st_mtime
logging.debug("[%s]:first building succeed!" % (time.strftime('%Y-%m-%d %H%M%S', time.localtime(time.time()))))
while 1:
sleep(30)
if os.stat("nginx.json").st_mtime != nginx_time or os.stat("rinetd.json").st_mtime != rinetd_time:
commands.getstatusoutput("sudo bash rebuild.sh")
nginx_time = os.stat("nginx.json").st_mtime
rinetd_time = os.stat("rinetd.json").st_mtime
logging.debug(
"[%s]:rebuilding succeed!" % (time.strftime('%Y-%m-%d %H%M%S', time.localtime(time.time()))))
except Exception, e:
logging.debug(str(e.message))
logging.debug("error while rebuilding\n")
if __name__ == '__main__':
daemonize()
main()