restart on sig 1
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
#!/usr/bin/env python
|
||||
# $Id: hbd,v 1.12 2006/11/07 19:41:11 andreas Exp $
|
||||
# $Id: hbd,v 1.13 2006/11/07 20:21:43 andreas Exp $
|
||||
# Wait for heartbeat messages and act on them (or their absence)
|
||||
#
|
||||
import time, os, string, sys, socket, atexit, select, SocketServer, getopt, signal
|
||||
import time, os, string, sys, socket, atexit, select, SocketServer, getopt, signal, cPickle
|
||||
|
||||
False=0
|
||||
True=1
|
||||
LOGFILE="/home/andreas/public_html/messages/andreas"
|
||||
|
||||
hosts={}
|
||||
htab={}
|
||||
|
||||
msgs=[]
|
||||
|
||||
num=0
|
||||
upcount=0
|
||||
PORT=50003
|
||||
@@ -22,7 +26,6 @@ GRACE=2
|
||||
|
||||
visual=0
|
||||
|
||||
msgs=[]
|
||||
|
||||
stdscr=None
|
||||
win=None
|
||||
@@ -33,9 +36,11 @@ msgwHeight=10
|
||||
def handler(signum, frame):
|
||||
global up, sig
|
||||
sig=signum
|
||||
if verbose: print "signal: %s up: %d" % (sig, up)
|
||||
if up == 0:
|
||||
return
|
||||
sys.exit(0)
|
||||
up=0
|
||||
# sys.exit(0)
|
||||
|
||||
class NullDevice:
|
||||
def write(self, s):
|
||||
@@ -205,7 +210,7 @@ def displayaddr(h, refresh=1):
|
||||
#
|
||||
def displaybody():
|
||||
for h in hosts.keys():
|
||||
win.addstr(hosts[h].num+1, 1, "%-24s" % (h))
|
||||
win.addstr(hosts[h].num+1, 1, "%-25s" % (h))
|
||||
if hosts[h].addr != None:
|
||||
displayaddr(h, 0)
|
||||
if hosts[h].statetime != None:
|
||||
@@ -423,12 +428,13 @@ class HtmlHandler(SocketServer.BaseRequestHandler):
|
||||
|
||||
helpflag=False
|
||||
forground=False
|
||||
restart=None
|
||||
optlist=[]
|
||||
args=[]
|
||||
home=os.environ['HOME']
|
||||
configfile="%s/.hbrc" % home
|
||||
try:
|
||||
optlist, args = getopt.getopt(sys.argv[1:], 'c:dfhv')
|
||||
optlist, args = getopt.getopt(sys.argv[1:], 'c:dfhR:v')
|
||||
except:
|
||||
helpflag=True
|
||||
|
||||
@@ -441,6 +447,8 @@ for o,a in optlist:
|
||||
forground=True
|
||||
elif o == '-h':
|
||||
helpflag=True
|
||||
elif o == '-R':
|
||||
restart=a
|
||||
elif o == '-v':
|
||||
verbose=True
|
||||
|
||||
@@ -521,17 +529,26 @@ if len(args) != 0:
|
||||
if verbose: print "notice: logging to %s" % logfile
|
||||
logf=initlog(logfile)
|
||||
|
||||
if restart:
|
||||
pickf=open(restart, 'r')
|
||||
pick=cPickle.Unpickler(pickf)
|
||||
hosts=pick.load()
|
||||
htab=pick.load()
|
||||
msgs=pick.load()
|
||||
pickf.close()
|
||||
|
||||
now=time.time()
|
||||
startsec=int(now) % interval
|
||||
|
||||
htab={}
|
||||
if visual:
|
||||
import curses
|
||||
initcurses()
|
||||
display()
|
||||
stdscr.nodelay(1)
|
||||
|
||||
if verbose: log("Starting")
|
||||
if verbose:
|
||||
if restart: log("Restarting")
|
||||
else: log("Starting")
|
||||
atexit.register(on_exit)
|
||||
|
||||
ilist=[]
|
||||
@@ -544,12 +561,13 @@ serv=SocketServer.TCPServer((hbd_host,hbd_port),HtmlHandler)
|
||||
ilist.append(serv.fileno())
|
||||
|
||||
if not forground:
|
||||
pid=os.fork()
|
||||
if pid > 0:
|
||||
if verbose:
|
||||
print "daemoinizing... pid=%d" % pid
|
||||
sys.exit(0)
|
||||
|
||||
if not restart:
|
||||
pid=os.fork()
|
||||
if pid > 0:
|
||||
if verbose:
|
||||
print "daemoinizing... pid=%d" % pid
|
||||
sys.exit(0)
|
||||
|
||||
verbose=False
|
||||
os.close(0)
|
||||
os.close(1)
|
||||
@@ -616,4 +634,14 @@ while up:
|
||||
|
||||
|
||||
if sig == signal.SIGHUP:
|
||||
os.execv(sys.argv[0],sys.argv+['-R'])
|
||||
sock.close()
|
||||
serv.socket.close()
|
||||
pickf=open("/tmp/pick1", 'w')
|
||||
pick=cPickle.Pickler(pickf)
|
||||
pick.dump(hosts)
|
||||
pick.dump(htab)
|
||||
pick.dump(msgs)
|
||||
pickf.close()
|
||||
|
||||
os.execv(sys.argv[0],sys.argv+['-R', "/tmp/pick1"])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user