File: auth2db-daemon

package info (click to toggle)
auth2db 0.2.5-2%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 6,508 kB
  • ctags: 1,090
  • sloc: python: 2,280; php: 2,161; sh: 174; sql: 157; makefile: 81
file content (71 lines) | stat: -rw-r--r-- 1,751 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python

import os
import sys

NAME="auth2db"
PIDFILE="/var/run/"+NAME+".pid"

def createDaemon():
	'''create auth2db daemon...'''
	
	# create - fork 1
	try:
		if os.fork() > 0: os._exit(0) # exit father...
	except OSError, error:
		print 'fork #1 failed: %d (%s)' % (error.errno, error.strerror)
		os._exit(1)

	# it separates the son from the father
	os.chdir('/')
	os.setsid()
	os.umask(0)

	# create - fork 2
	try:
		pid = os.fork()
		if pid > 0:
                    fd = open(PIDFILE, 'w')
                    fd.write(str(pid))
                    fd.flush()
                    print 'START auth2db daemon PID %d' % pid
                    os._exit(0)
	except OSError, error:
            print 'fork #2 failed: %d (%s)' % (error.errno, error.strerror)
            os._exit(1)

	daemonAuth2db() # function daemon
	
def daemonAuth2db():

	import time

	while True:
            os.system("auth2db > /dev/null")
            os.system("auth2db-alert > /dev/null")
            time.sleep(2)
            
	
if __name__ == '__main__':
    if len(sys.argv) > 1 and sys.argv[1] == "start":
        
        existepid = os.path.exists(PIDFILE)
        if existepid == 0:
            createDaemon()
        else:
            print "auth2db is already running..."
            sys.exit() 
    
    elif len(sys.argv) > 1 and sys.argv[1] == "stop":
        
        existepid = os.path.exists(PIDFILE)
        if existepid:
            fd = open( PIDFILE )
            file_pid = fd.readline()
            print 'STOP auth2db daemon PID '+str(file_pid)
            os.system("kill "+file_pid)
            os.system("rm "+PIDFILE)
        else:
            print "auth2db is not running..."
    else:
        print "Usage: auth2db-daemon start|stop"