basichttp server (забыл git add .)
[chanspy.git] / daemon.shit / chanspyd
blobdfe3f8b39fe18103012c86ed7a2ed8ce4e593a64
1 #!/usr/bin/env 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 config
26 import services
28 #----------------------------------------------
29 def main():
30 """Main function. Write pid file and run service."""
32 if os.path.exists(PID):
33 sys.stderr.write('chanspyd\'s already executed. If not then remove %s.\n' %PID)
34 sys.exit(2)
36 if write_pid:
37 try:
38 f = open(PID, 'w')
39 f.write(str(os.getpid()))
40 f.close()
41 except:
42 sys.stderr.write('Can\'t write pid to file.\n')
43 sys.exit(2)
44 signal.signal(signal.SIGTERM, sigTermCB)
46 services.start(logfile, chans)
48 #----------------------------------------------
49 def help():
50 """Print help message."""
52 help = ('Usage: %s [OPTIONS]' %sys.argv[0]) +\
53 '\n\nOptions:\n' +\
54 '-h --help help\n' +\
55 '-d --daemon run in background\n' +\
56 '-k --kill kill daemon'
57 print(help)
59 #----------------------------------------------
60 def error():
61 """Invalid option."""
63 error = ('%s: invalid option.\n' %sys.argv[0]) +\
64 'Try `%s --help\' for more information.\n' %sys.argv[0]
65 sys.stderr.write(error)
66 sys.exit(1)
68 #----------------------------------------------
69 def kill():
70 """Kill process by pid."""
72 try:
73 f = open(PID)
74 pid = f.read()
75 f.close()
76 except:
77 sys.stderr.write('chanspyd is not running\n')
78 sys.exit(2)
79 else:
80 try:
81 os.kill(int(pid), signal.SIGTERM)
82 except:
83 sys.stderr.write('Cat\'t kill %s.\n' %pid)
84 sys.exit(2)
86 #----------------------------------------------
87 def sigTermCB(signum, frame):
88 """Removing pid and exiting on SIGTERM."""
90 try:
91 os.remove(PID)
92 finally:
93 services.stop()
95 ###############################################
96 # default settings
97 config_path = 'configs/config.yaml'
98 write_pid = False
99 PID = 'chanspyd.pid'
100 chans = []
102 # reading options
103 options = config.read_config(config_path)
104 try:
105 write_pid = options['daemon']['write_pid']
106 PID = options['daemon']['PID']
107 chans = options['chans']
108 except:
109 pass
111 logfile = '/dev/stdout'
113 if len(sys.argv) == 2:
114 if sys.argv[1] == '-h' or sys.argv[1] == '--help':
115 help()
117 elif sys.argv[1] == '-d' or sys.argv[1] == '--daemon' :
118 try: logfile = options['daemon']['logfile']
119 except: logfile = 'chanspyd.log'
121 pid = os.fork()
122 if pid == 0:
123 os.setsid()
124 pid = os.fork()
125 if pid == 0:
126 main()
128 elif sys.argv[1] == '-k' or sys.argv[1] == '--kill':
129 kill()
131 else:
132 error()
133 elif len(sys.argv) == 1:
134 main()
135 else:
136 error()