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 ###############################################
28 #----------------------------------------------
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
)
39 f
.write(str(os
.getpid()))
42 sys
.stderr
.write('Can\'t write pid to file.\n')
44 signal
.signal(signal
.SIGTERM
, sigTermCB
)
46 services
.start(logfile
, chans
)
48 #----------------------------------------------
50 """Print help message."""
52 help = ('Usage: %s [OPTIONS]' %sys
.argv
[0]) +\
55 '-d --daemon run in background\n' +\
56 '-k --kill kill daemon'
59 #----------------------------------------------
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
)
68 #----------------------------------------------
70 """Kill process by pid."""
77 sys
.stderr
.write('chanspyd is not running\n')
81 os
.kill(int(pid
), signal
.SIGTERM
)
83 sys
.stderr
.write('Cat\'t kill %s.\n' %pid
)
86 #----------------------------------------------
87 def sigTermCB(signum
, frame
):
88 """Removing pid and exiting on SIGTERM."""
95 ###############################################
97 config_path
= 'configs/config.yaml'
103 options
= config
.read_config(config_path
)
105 write_pid
= options
['daemon']['write_pid']
106 PID
= options
['daemon']['PID']
107 chans
= options
['chans']
111 logfile
= '/dev/stdout'
113 if len(sys
.argv
) == 2:
114 if sys
.argv
[1] == '-h' or sys
.argv
[1] == '--help':
117 elif sys
.argv
[1] == '-d' or sys
.argv
[1] == '--daemon' :
118 try: logfile
= options
['daemon']['logfile']
119 except: logfile
= 'chanspyd.log'
128 elif sys
.argv
[1] == '-k' or sys
.argv
[1] == '--kill':
133 elif len(sys
.argv
) == 1: