From 1621311b66c33749caafc6b0291412c6b4d376a3 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Wed, 7 Dec 2011 12:10:34 +0200 Subject: skytools.signal_pidfile: ignore empty pidfile, some cleanups Empty pidfile can happen if old process failed to write it for some reason. --- python/skytools/scripting.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'python/skytools/scripting.py') diff --git a/python/skytools/scripting.py b/python/skytools/scripting.py index b0badda9..583c6464 100644 --- a/python/skytools/scripting.py +++ b/python/skytools/scripting.py @@ -36,10 +36,14 @@ def signal_pidfile(pidfile, sig): Returns True is successful, False if pidfile does not exist or process itself is dead. Any other errors will passed - as exceptions. - """ + as exceptions.""" + + ln = '' try: - pid = int(open(pidfile, 'r').readline()) + f = open(pidfile, 'r') + ln = f.readline().strip() + f.close() + pid = int(ln) os.kill(pid, sig) return True except IOError, ex: @@ -49,6 +53,10 @@ def signal_pidfile(pidfile, sig): if ex.errno != errno.ESRCH: raise except ValueError, ex: + # this leaves slight race when someone is just creating the file, + # but more common case is old empty file. + if not ln: + return False raise ValueError('Corrupt pidfile: %s' % pidfile) return False -- cgit v1.2.3