summaryrefslogtreecommitdiff
path: root/python/skytools/scripting.py
diff options
context:
space:
mode:
authorMarko Kreen2009-10-14 13:47:31 +0000
committerMarko Kreen2009-10-14 13:47:31 +0000
commit9dcab958bdaadb2787027b9b10af362a0bc2f861 (patch)
treee1360f38500014e4a30e41ff88e7116ebe98bca4 /python/skytools/scripting.py
parent66c72793edd322c55dd02744abd8fab822e211b4 (diff)
skytools.DBScript: safer pidfile writing
- signal_pidfile: Clarify ValueError error message. Thrown usually on empty pidfiles, the error messega will now mention the pidfile name. - run_single_process: restructure pidfile writing, so that the pidfile is removed if .write() failed, but not when open() failed. This should avoid the chance that empty pidfiles are hanging around.
Diffstat (limited to 'python/skytools/scripting.py')
-rw-r--r--python/skytools/scripting.py28
1 files changed, 15 insertions, 13 deletions
diff --git a/python/skytools/scripting.py b/python/skytools/scripting.py
index 83e050eb..0d4b0fab 100644
--- a/python/skytools/scripting.py
+++ b/python/skytools/scripting.py
@@ -42,7 +42,7 @@ def signal_pidfile(pidfile, sig):
"""Send a signal to process whose ID is located in pidfile.
Read only first line of pidfile to support multiline
- pifiles like postmaster.pid.
+ pidfiles like postmaster.pid.
Returns True is successful, False if pidfile does not exist
or process itself is dead. Any other errors will passed
@@ -58,6 +58,8 @@ def signal_pidfile(pidfile, sig):
except OSError, ex:
if ex.errno != errno.ESRCH:
raise
+ except ValueError, ex:
+ raise ValueError('Corrupt pidfile: %s' % pidfile)
return False
#
@@ -90,12 +92,6 @@ def daemonize():
# Pidfile locking+cleanup & daemonization combined
#
-def _write_pidfile(pidfile):
- pid = os.getpid()
- f = open(pidfile, 'w')
- f.write(str(pid))
- f.close()
-
def run_single_process(runnable, daemon, pidfile):
"""Run runnable class, possibly daemonized, locked on pidfile."""
@@ -107,17 +103,23 @@ def run_single_process(runnable, daemon, pidfile):
else:
print("Ignoring stale pidfile")
- # daemonize if needed and write pidfile
+ # daemonize if needed
if daemon:
daemonize()
- if pidfile:
- _write_pidfile(pidfile)
-
- # run and clean pidfile later
+
+ # clean only own pidfile
+ own_pidfile = False
+
try:
+ if pidfile:
+ f = open(pidfile, 'w')
+ own_pidfile = True
+ f.write(str(os.getpid()))
+ f.close()
+
runnable.run()
finally:
- if pidfile:
+ if own_pidfile:
try:
os.remove(pidfile)
except: pass