summaryrefslogtreecommitdiffstats
path: root/bin/utils/daemon.py
blob: 3744222f28300573c203c9578e864c493e6024b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python

'''
	This module is used to fork the current process into a daemon.
	Almost none of this is necessary (or advisable) if your daemon 
	is being started by inetd. In that case, stdin, stdout and stderr are 
	all set up for you to refer to the network connection, and the fork()s 
	and session manipulation should not be done (to avoid confusing inetd). 
	Only the chdir() and umask() steps remain as useful.
	References:
		UNIX Programming FAQ
		    1.7 How do I get my program to act like a daemon?
		        http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
		Advanced Programming in the Unix Environment
		    W. Richard Stevens, 1992, Addison-Wesley, ISBN 0-201-56317-7.

	History:
	  2001/07/10 by J�rgen Hermann
	  2002/08/28 by Noah Spurrier
	  2003/02/24 by Clark Evans
	  
	  http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
'''
import sys, os, time
from signal import SIGTERM

def daemonize(stdout='/dev/null', stderr=None, stdin='/dev/null', pidfile=None, startmsg=None):
	''' 
		This forks the current process into a daemon.
		The stdin, stdout, and stderr arguments are file names that
		will be opened and be used to replace the standard file descriptors
		in sys.stdin, sys.stdout, and sys.stderr.
		These arguments are optional and default to /dev/null.
		Note that stderr is opened unbuffered, so
		if it shares a file with stdout then interleaved output
		may not appear in the order that you expect.
	'''
	# Do first fork.
	try: 
		pid = os.fork() 
		if pid > 0: sys.exit(0) # Exit first parent.
	except OSError, e: 
		sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
		sys.exit(1)
		
	# Decouple from parent environment.
	os.chdir("/") 
	os.umask(0) 
	os.setsid() 
	
	# Do second fork.
	try: 
		pid = os.fork() 
		if pid > 0: sys.exit(0) # Exit second parent.
	except OSError, e: 
		sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
		sys.exit(1)
	
	# Open file descriptors and print start message
	if not stderr: stderr = stdout
	si = file(stdin, 'r')
	so = file(stdout, 'a+')
	se = file(stderr, 'a+', 0)
	pid = str(os.getpid())
	if startmsg:
		sys.stderr.write("\n%s\n" % startmsg % pid)
		sys.stderr.flush()
	if pidfile: file(pidfile,'w+').write("%s\n" % pid)
	
	# Redirect standard file descriptors.
	os.dup2(si.fileno(), sys.stdin.fileno())
	os.dup2(so.fileno(), sys.stdout.fileno())
	os.dup2(se.fileno(), sys.stderr.fileno())