make lock work if pid file is empty
This commit is contained in:
@@ -12,9 +12,7 @@ import errno
|
|||||||
|
|
||||||
PORT=50003
|
PORT=50003
|
||||||
INTERVAL=10
|
INTERVAL=10
|
||||||
False=0
|
DBG = True
|
||||||
True=1
|
|
||||||
DBG = False
|
|
||||||
|
|
||||||
sock=None
|
sock=None
|
||||||
|
|
||||||
@@ -31,6 +29,7 @@ class Flock:
|
|||||||
self.opid = None
|
self.opid = None
|
||||||
|
|
||||||
def lock(self):
|
def lock(self):
|
||||||
|
if DBG: print "lock it"
|
||||||
while 1:
|
while 1:
|
||||||
self.fd = None
|
self.fd = None
|
||||||
try:
|
try:
|
||||||
@@ -38,9 +37,10 @@ class Flock:
|
|||||||
except OSError, e:
|
except OSError, e:
|
||||||
if e.errno != errno.EEXIST:
|
if e.errno != errno.EEXIST:
|
||||||
raise
|
raise
|
||||||
|
if DBG: print "create error %s" % e.errno
|
||||||
except:
|
except:
|
||||||
raise
|
raise
|
||||||
|
if DBG: print "lock() - self.fd = '%s'" % self.fd
|
||||||
if not self.fd:
|
if not self.fd:
|
||||||
if self.oproc():
|
if self.oproc():
|
||||||
if DBG: print "process is alive"
|
if DBG: print "process is alive"
|
||||||
@@ -62,8 +62,13 @@ class Flock:
|
|||||||
def setopid(self):
|
def setopid(self):
|
||||||
try:
|
try:
|
||||||
self.opid=open(self.lock_file).readline()
|
self.opid=open(self.lock_file).readline()
|
||||||
|
if DBG: print "setopid = '%s'" % self.opid
|
||||||
|
if self.opid == '':
|
||||||
|
os.remove(self.lock_file)
|
||||||
except:
|
except:
|
||||||
pass
|
self.opid = ''
|
||||||
|
if self.opid == '':
|
||||||
|
self.opid = None
|
||||||
|
|
||||||
|
|
||||||
def oproc(self):
|
def oproc(self):
|
||||||
@@ -110,6 +115,7 @@ def socksend(msg, tohost):
|
|||||||
if sock == None:
|
if sock == None:
|
||||||
sock=getsock(tohost[0])
|
sock=getsock(tohost[0])
|
||||||
sock.sendto(msg, tohost)
|
sock.sendto(msg, tohost)
|
||||||
|
if verbose: print "msg %s sent" % msg
|
||||||
|
|
||||||
|
|
||||||
msgonly=False
|
msgonly=False
|
||||||
@@ -238,6 +244,7 @@ if len(msgboot) > 0:
|
|||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if verbose: print "msgboot done msgonly=%s" % msgonly
|
||||||
if msgonly:
|
if msgonly:
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
@@ -265,7 +272,9 @@ if daemon:
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
lock=Flock('/tmp/hbc.pid')
|
lock=Flock('/tmp/hbc.pid')
|
||||||
|
if DBG: print "get lock"
|
||||||
if lock.lock():
|
if lock.lock():
|
||||||
|
if DBG: print "got lock"
|
||||||
break
|
break
|
||||||
if not lock.oproc():
|
if not lock.oproc():
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user