Write PID into the lock file

This commit is contained in:
Peter Šurda 2017-07-28 09:39:49 +02:00
parent 501f07dd34
commit e7382b7714
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87

View File

@ -44,7 +44,7 @@ class singleinstance:
# file already exists, we try to remove (in case previous execution was interrupted) # file already exists, we try to remove (in case previous execution was interrupted)
if os.path.exists(self.lockfile): if os.path.exists(self.lockfile):
os.unlink(self.lockfile) os.unlink(self.lockfile)
self.fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR) self.fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR | os.O_TRUNC)
except OSError: except OSError:
type, e, tb = sys.exc_info() type, e, tb = sys.exc_info()
if e.errno == 13: if e.errno == 13:
@ -52,6 +52,9 @@ class singleinstance:
sys.exit(-1) sys.exit(-1)
print(e.errno) print(e.errno)
raise raise
else:
pidLine = "%i\n" % self.lockPid
self.fd.write(pidLine)
else: # non Windows else: # non Windows
self.fp = open(self.lockfile, 'w') self.fp = open(self.lockfile, 'w')
try: try:
@ -63,6 +66,10 @@ class singleinstance:
except IOError: except IOError:
print 'Another instance of this application is already running' print 'Another instance of this application is already running'
sys.exit(-1) sys.exit(-1)
else:
pidLine = "%i\n" % self.lockPid
self.fp.write(pidLine)
self.fp.flush()
def cleanup(self): def cleanup(self):
if not self.initialized: if not self.initialized: