summaryrefslogtreecommitdiffstats
path: root/vstf/vstf/common/daemon.py
diff options
context:
space:
mode:
Diffstat (limited to 'vstf/vstf/common/daemon.py')
-rwxr-xr-xvstf/vstf/common/daemon.py63
1 files changed, 32 insertions, 31 deletions
diff --git a/vstf/vstf/common/daemon.py b/vstf/vstf/common/daemon.py
index 1085d36c..35933dad 100755
--- a/vstf/vstf/common/daemon.py
+++ b/vstf/vstf/common/daemon.py
@@ -20,45 +20,46 @@ class Daemon(object):
Usage: subclass the Daemon class and override the run() method
"""
+
def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
super(Daemon, self).__init__()
self.stdin = stdin
self.stdout = stdout
self.stderr = stderr
self.pidfile = pidfile
-
+
def daemonize(self):
"""
do the UNIX double-fork magic, see Stevens' "Advanced
Programming in the UNIX Environment" for details (ISBN 0201563177)
http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
"""
- try:
- pid = os.fork()
+ try:
+ pid = os.fork()
if pid > 0:
- sys.exit(0)
- except OSError, e:
+ sys.exit(0)
+ except OSError, e:
LOG.error("fork #1 failed: %(errno)s, %(strerror)s",
- {'errno':e.errno, 'strerror': e.strerror})
+ {'errno': e.errno, 'strerror': e.strerror})
sys.exit(1)
-
+
# decouple from parent environment
- os.chdir("/")
- os.setsid()
- os.umask(0)
-
+ os.chdir("/")
+ os.setsid()
+ os.umask(0)
+
# do second fork
- try:
- pid = os.fork()
+ try:
+ pid = os.fork()
if pid > 0:
# exit from second parent
- sys.exit(0)
- except OSError, e:
+ sys.exit(0)
+ except OSError, e:
LOG.error("fork #1 failed: %(errno)s, %(strerror)s",
- {'errno':e.errno, 'strerror': e.strerror})
- sys.exit(1)
-
- # redirect standard file descriptors
+ {'errno': e.errno, 'strerror': e.strerror})
+ sys.exit(1)
+
+ # redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
si = file(self.stdin, 'r')
@@ -67,12 +68,12 @@ class Daemon(object):
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
-
+
# write pidfile
atexit.register(self.delpid)
pid = str(os.getpid())
- file(self.pidfile,'w+').write("%s\n" % pid)
-
+ file(self.pidfile, 'w+').write("%s\n" % pid)
+
def delpid(self):
os.remove(self.pidfile)
@@ -80,15 +81,15 @@ class Daemon(object):
"""
Start the daemon
"""
-
+
# Check for a pidfile to see if the daemon already runs
try:
- pf = file(self.pidfile,'r')
+ pf = file(self.pidfile, 'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
-
+
if pid:
message = "pidfile %s already exist. Daemon already running?\n"
sys.stderr.write(message % self.pidfile)
@@ -104,16 +105,16 @@ class Daemon(object):
"""
# Get the pid from the pidfile
try:
- pf = file(self.pidfile,'r')
+ pf = file(self.pidfile, 'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
-
+
if not pid:
message = "pidfile %s does not exist. Daemon not running?\n"
sys.stderr.write(message % self.pidfile)
- return # not an error in a restart
+ return # not an error in a restart
# Try killing the daemon process
try:
@@ -144,10 +145,10 @@ class Daemon(object):
"""
pass
-
+
def daemon_die(self):
- """You should this method when you shutdown daemon
+ """You should override this method when you shutdown daemon
this func will be call by stop() before kill the process
"""
- pass \ No newline at end of file
+ pass