From d6037a338b6407b6ac9fd77a26bd3c721d80f109 Mon Sep 17 00:00:00 2001 From: RedKrieg Date: Fri, 16 Dec 2011 21:02:57 -0500 Subject: [PATCH] Change status function to check cmdline of the pid referenced in the file, removing the pidfile if cmdline doesn't match the calling script name. This change resolves problems with a stale pid after reboot, as well as a pid file pointing to another process's pid. --- yapdi.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/yapdi.py b/yapdi.py index c9db7bb..4968ac2 100644 --- a/yapdi.py +++ b/yapdi.py @@ -120,6 +120,23 @@ def status(self): pf = file(self.pidfile,'r') pid = int(pf.read().strip()) pf.close() + + # See if the pidfile matches the calling script name + cmdlinepath = '/proc/%d/cmdline' % pid + if os.path.exists(cmdlinepath): + cmdlinefile = open(cmdlinepath, 'r') + cmdline = cmdlinefile.read() + cmdlinefile.close() + + # The pid in our file isn't this script + if not sys.argv[0].split('/')[-1] in cmdline: + self.delpid() + pid = None + + # The pid from our pidfile isn't running + else: + self.delpid() + pid = None except IOError: pid = None return pid