Subversion Repositories SmartDukaan

Rev

Rev 3894 | Rev 19284 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3894 Rev 19283
Line 8... Line 8...
8
Some of the references within the code comments are no longer available.
8
Some of the references within the code comments are no longer available.
9
 
9
 
10
'''
10
'''
11
 
11
 
12
import sys, os, time, atexit
12
import sys, os, time, atexit
13
from signal import SIGTERM 
13
from signal import SIGTERM
-
 
14
import logging 
14
 
15
 
15
class Daemon:
16
class Daemon:
16
	"""
17
	"""
17
	A generic daemon class.
18
	A generic daemon class.
18
	
19
	
Line 21... Line 22...
21
	def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
22
	def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
22
		self.stdin = stdin
23
		self.stdin = stdin
23
		self.stdout = stdout
24
		self.stdout = stdout
24
		self.stderr = stderr
25
		self.stderr = stderr
25
		self.pidfile = pidfile
26
		self.pidfile = pidfile
-
 
27
		configure_logging(stdout)
26
	
28
	
27
	def daemonize(self):
29
	def daemonize(self):
28
		"""
30
		"""
29
		do the UNIX double-fork magic, see Stevens' "Advanced 
31
		do the UNIX double-fork magic, see Stevens' "Advanced 
30
		Programming in the UNIX Environment" for details (ISBN 0201563177)
32
		Programming in the UNIX Environment" for details (ISBN 0201563177)
Line 56... Line 58...
56
	
58
	
57
		# redirect standard file descriptors
59
		# redirect standard file descriptors
58
		sys.stdout.flush()
60
		sys.stdout.flush()
59
		sys.stderr.flush()
61
		sys.stderr.flush()
60
		si = file(self.stdin, 'r')
62
		si = file(self.stdin, 'r')
-
 
63
		
-
 
64
		so = os.open(self.stdout, os.O_RDWR)
61
		so = file(self.stdout, 'a+')
65
		#so = file(self.stdout, 'a+')
62
		se = file(self.stderr, 'a+', 0)
66
		se = os.open(self.stderr, os.O_RDWR)
63
		os.dup2(si.fileno(), sys.stdin.fileno())
67
		os.dup2(si.fileno(), sys.stdin.fileno())
64
		os.dup2(so.fileno(), sys.stdout.fileno())
68
		os.dup2(so.fileno(), sys.stdout.fileno())
65
		os.dup2(se.fileno(), sys.stderr.fileno())
69
		os.dup2(se.fileno(), sys.stderr.fileno())
66
	
70
	
67
		# write pidfile
71
		# write pidfile
Line 135... Line 139...
135
		"""
139
		"""
136
		You should override this method when you subclass Daemon. It will be called after the process has been
140
		You should override this method when you subclass Daemon. It will be called after the process has been
137
		daemonized by start() or restart().
141
		daemonized by start() or restart().
138
		"""
142
		"""
139
		pass
143
		pass
-
 
144
def configure_logging(logfile):
-
 
145
    fh = logging.FileHandler(logfile, 'w')
-
 
146
    formatter = logging.Formatter(logging.BASIC_FORMAT)
-
 
147
    fh.setFormatter(formatter)
-
 
148
    root = logging.getLogger()
-
 
149
    root.setLevel(logging.ERROR)
-
 
150
    root.addHandler(fh)
140
151