make lock work if pid file is empty

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