ChanPool -> Chan -> Board -> Thread base
[chanspy.git] / daemon / chanspyd
blob320af9f57aeaea98b7e77b9c92c4ca65d4d65691
1 #!/usr/bin/python
3 ###############################################
4 # chanspyd - chanspy daemon
5 # Copyright (C) 2008 anonymous
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 ###############################################
21 import sys
22 import os
23 import signal
25 import services
27 PID = 'chanspyd.pid'
29 def main():
30 if os.path.exists(PID):
31 sys.stderr.write('chanspyd\'s already executed. If not then remove %s.\n' %PID)
32 sys.exit(2)
34 try:
35 pass
36 #f = open(PID, 'w')
37 #f.write(str(os.getpid()))
38 #f.close()
39 except:
40 sys.stderr.write('can\'t write pid to file\n')
41 sys.exit(2)
43 services.start(logfile)
45 def help():
46 help = ('Usage: %s OPTIONS' %sys.argv[0]) +\
47 '\n\nOptions:\n' +\
48 '-h --help help\n' +\
49 '-d --daemon run in background\n' +\
50 '-k --kill kill daemon'
51 print(help)
53 def error():
54 error = ('%s: invalid option.\n' %sys.argv[0]) +\
55 'Try `%s --help\' for more information.\n' %sys.argv[0]
56 sys.stderr.write(error)
57 sys.exit(1)
59 def kill():
60 try:
61 f = open(PID)
62 pid = f.read()
63 f.close()
64 except:
65 sys.stderr.write('chanspyd is not running\n')
66 sys.exit(2)
67 else:
68 try:
69 os.kill(int(pid), signal.SIGTERM)
70 except:
71 sys.stderr.write('cat\'t kill %s\n' %pid)
72 sys.exit(2)
74 ###############################################
75 logfile = '/dev/stdout'
76 if len(sys.argv) == 2:
77 if sys.argv[1] == '-h' or sys.argv[1] == '--help':
78 help()
80 elif sys.argv[1] == '-d' or sys.argv[1] == '--daemon' :
81 logfile = 'chanspyd.log'
83 pid = os.fork()
84 if pid == 0:
85 os.setsid()
86 pid = os.fork()
87 if pid == 0:
88 main()
90 elif sys.argv[1] == '-k' or sys.argv[1] == '--kill':
91 kill()
93 else:
94 error()
95 elif len(sys.argv) == 1:
96 main()
97 else:
98 error()