From e9b31f439a14be0c6a562108ec947a2d78e06107 Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Thu, 17 Jun 2010 14:51:31 +0200 Subject: [PATCH] Reorder daemonizing (creation of PID file) If the pipe is NOT writeable you should see an error message instead of just a not running daemon. Signed-off-by: Sven Velt --- nagixsc/__init__.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/nagixsc/__init__.py b/nagixsc/__init__.py index 8f11a42..825d41d 100644 --- a/nagixsc/__init__.py +++ b/nagixsc/__init__.py @@ -437,7 +437,7 @@ def daemonize(pidfile=None, stdin='/dev/null', stdout='/dev/null', stderr='/dev/ if pid > 0: sys.exit(0) except OSError, e: - sys.stderr.write("1st fork failed: (%d) %sn" % (e.errno, e.strerror)) + sys.stderr.write("1st fork failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # Prepare 2nd fork os.chdir("/") @@ -449,8 +449,18 @@ def daemonize(pidfile=None, stdin='/dev/null', stdout='/dev/null', stderr='/dev/ if pid > 0: sys.exit(0) except OSError, e: - sys.stderr.write("2nd fork failed: (%d) %sn" % (e.errno, e.strerror)) + sys.stderr.write("2nd fork failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) + + # Try to write PID file + if pidfile: + pid = str(os.getpid()) + try: + file(pidfile, 'w+').write('%s\n' % pid) + except IOError: + sys.stderr.write("Could not write PID file, exiting...\n") + sys.exit(1) + # Redirect stdin, stdout, stderr sys.stdout.flush() sys.stderr.flush() @@ -461,10 +471,6 @@ def daemonize(pidfile=None, stdin='/dev/null', stdout='/dev/null', stderr='/dev/ os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) - if pidfile: - pid = str(os.getpid()) - file(pidfile, 'w+').write('%s\n' % pid) - return ############################################################################## -- 2.30.2