1
0
Fork 0

Write pidfile even if running in foreground (for tuned-adm to work)

This commit is contained in:
Jaroslav Škarvada 2012-10-24 14:30:21 +02:00
parent 2edd5852cb
commit 466dc4c767
2 changed files with 16 additions and 9 deletions

View file

@ -79,5 +79,7 @@ if __name__ == "__main__":
else:
log.critical("cannot daemonize")
sys.exit(1)
else:
tuned.utils.write_pidfile()
app.run()

View file

@ -1,17 +1,19 @@
__all__ = ["daemonize"]
__all__ = ["daemonize", "write_pidfile"]
import tuned.logs
import os
import signal
import sys
PIDFILE = "/var/run/tuned/tuned.pid"
log = tuned.logs.get()
def handle_signal(signals, handler, pass_args = True):
for s in signals:
signal.signal(s, handler)
def daemonize(timeout, pidfile = "/var/run/tuned/tuned.pid"):
def daemonize(timeout, pidfile = PIDFILE):
"""
Perform current process daemonization. Kills current SIGALRM, SIGUSR1, and SIGUSR2 signal handlers.
"""
@ -28,6 +30,15 @@ def daemonize(timeout, pidfile = "/var/run/tuned/tuned.pid"):
handle_signal([signal.SIGALRM, signal.SIGUSR1, signal.SIGUSR2], signal.SIG_DFL)
return result
def write_pidfile(pidfile = PIDFILE):
try:
if not os.path.exists(os.path.dirname(pidfile)):
os.makedirs(os.path.dirname(pidfile))
with open(pidfile, "w") as f:
f.write(str(os.getpid()))
except (OSError,IOError) as e:
log.critical("Cannot write the PID to %s: %s" % (pidfile, str(e)))
def _daemonize_fork(timeout, pidfile):
try:
pid = os.fork()
@ -57,13 +68,7 @@ def _daemonize_fork(timeout, pidfile):
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
try:
if not os.path.exists(os.path.dirname(pidfile)):
os.makedirs(os.path.dirname(pidfile))
with open(pidfile, "w") as f:
f.write(str(os.getpid()))
except (OSError,IOError) as e:
log.critical("Cannot write the PID to %s: %s" % (pidfile, str(e)))
write_pidfile(pidfile)
return True