Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
4 # Copyright (C) 2006-2012 Eyeo GmbH | 4 # Copyright (C) 2006-2012 Eyeo GmbH |
5 # | 5 # |
6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
9 # | 9 # |
10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
14 # | 14 # |
15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
18 import os | 18 import os, subprocess |
19 from sitescripts.utils import get_config | 19 from sitescripts.utils import get_config |
20 | 20 |
21 def _get_services(): | 21 def _get_services(): |
22 config = get_config() | 22 config = get_config() |
23 section_name = "keep_alive_services" | 23 section_name = "keep_alive_services" |
24 section_keys = config.options(section_name) | 24 section_keys = config.options(section_name) |
25 default_keys = config.defaults().keys() | 25 default_keys = config.defaults().keys() |
26 keys = set(section_keys) - set(default_keys) | 26 keys = set(section_keys) - set(default_keys) |
27 | 27 |
28 services = {} | 28 services = {} |
29 for key in keys: | 29 for key in keys: |
30 services[key] = config.get(section_name, key) | 30 services[key] = config.get(section_name, key) |
31 return services | 31 return services |
32 | |
33 def _files_exist(files): | |
34 for file in files: | |
35 if not os.path.exists(file): | |
36 print "'%s' does not exist." % file | |
37 return False | |
38 return True | |
39 | 32 |
40 def _process_running(pid): | 33 def _process_running(pid): |
41 return True | 34 try: |
Wladimir Palant
2012/11/29 15:01:46
That still needs to be written, right?
Felix Dahlke
2012/11/30 09:49:32
Um, yes. That's sort of important :) Done.
| |
35 os.kill(pid, 0) | |
36 return True | |
37 except OSError: | |
38 return False | |
42 | 39 |
43 if __name__ == "__main__": | 40 if __name__ == "__main__": |
44 services = _get_services() | 41 services = _get_services() |
45 for service in services.keys(): | 42 for service in services.keys(): |
46 init_file = "/etc/init.d/%s" % service | 43 pid_path = os.path.join("/var/run", services[service]) |
47 pid_file = "/var/run/%s" % services[service] | 44 if os.path.exists(pid_path): |
Wladimir Palant
2012/11/29 15:01:46
Use os.path.join() here?
Felix Dahlke
2012/11/30 09:49:32
Done.
| |
45 with open(pid_path) as file: | |
46 pid_string = file.read() | |
48 | 47 |
49 if not _files_exist([init_file, pid_file]): | 48 try: |
Wladimir Palant
2012/11/29 15:01:46
Actually, if the pid file doesn't exist it means t
Felix Dahlke
2012/11/30 09:49:32
Done.
| |
50 continue | 49 pid = int(pid_string.rstrip()) |
50 if _process_running(pid): | |
51 continue | |
52 except exceptions.ValueError: | |
53 print "'%s' is not a PID." % pid_string | |
51 | 54 |
52 pid_file_content = open(pid_file).read().rstrip() | 55 init_path = os.path.join("/etc/init.d", service) |
Wladimir Palant
2012/11/29 15:01:46
I generally prefer to close files explicitly...
Felix Dahlke
2012/11/30 09:49:32
Done.
| |
53 try: | 56 if not os.path.exists(init_path): |
54 pid = int(pid_file_content) | 57 print "%s does not exist, service is not running and cannot be started." % init_path |
55 except exceptions.ValueError: | |
56 print "'%s' is not a PID." % pid_file_content | |
57 continue | |
58 | |
59 if _process_running(pid): | |
60 continue | 58 continue |
61 | 59 |
62 print "%s is not running, starting ..." % service | 60 print "%s is not running, starting ..." % service |
Wladimir Palant
2012/11/29 15:01:46
You are aware that any output from a cron job resu
Felix Dahlke
2012/11/30 09:49:32
Yes, that's what I intended. A crashing service is
| |
63 subprocess.call([init_file, "start"]) | 61 subprocess.call([init_path, "start"]) |
LEFT | RIGHT |