summaryrefslogtreecommitdiff
path: root/python/skytools/scripting.py
diff options
context:
space:
mode:
authorMarko Kreen2011-12-07 10:10:34 +0000
committerMarko Kreen2011-12-07 10:10:34 +0000
commit1621311b66c33749caafc6b0291412c6b4d376a3 (patch)
treecc770722a19cb7c7554db415d179273702d86170 /python/skytools/scripting.py
parent245b5a79e88681529631edcc7f7e5b1cacfe1d04 (diff)
skytools.signal_pidfile: ignore empty pidfile, some cleanups
Empty pidfile can happen if old process failed to write it for some reason.
Diffstat (limited to 'python/skytools/scripting.py')
-rw-r--r--python/skytools/scripting.py14
1 files changed, 11 insertions, 3 deletions
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