 Issue 8963083:
  sitescripts: Script to keep services alive  (Closed)
    
  
    Issue 8963083:
  sitescripts: Script to keep services alive  (Closed) 
  | 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, subprocess | 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) | 
| 
Wladimir Palant
2012/11/30 10:46:07
Strange indentation here, one space instead of two
 
Felix Dahlke
2012/11/30 13:32:28
Done.
 | |
| 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 | 32 | 
| 33 def _process_running(pid): | 33 def _process_running(pid): | 
| 34 return os.path.isdir(os.path.join("/proc", str(pid))) | 34 try: | 
| 
Wladimir Palant
2012/11/30 10:46:07
I might have preferred os.kill(..., 0) but this wi
 
Felix Dahlke
2012/11/30 13:32:28
Didn't know that trick, using it now.
 | |
| 35 os.kill(pid, 0) | |
| 36 return True | |
| 37 except OSError: | |
| 38 return False | |
| 35 | 39 | 
| 36 if __name__ == "__main__": | 40 if __name__ == "__main__": | 
| 37 services = _get_services() | 41 services = _get_services() | 
| 38 for service in services.keys(): | 42 for service in services.keys(): | 
| 39 pid_path = os.path.join("/var/run", services[service]) | 43 pid_path = os.path.join("/var/run", services[service]) | 
| 40 if os.path.exists(pid_path): | 44 if os.path.exists(pid_path): | 
| 41 with open(pid_path) as file: pid_string = file.read() | 45 with open(pid_path) as file: | 
| 
Wladimir Palant
2012/11/30 10:46:07
Does this really have to be all on one line?
 
Felix Dahlke
2012/11/30 13:32:28
Done.
 | |
| 46 pid_string = file.read() | |
| 42 | 47 | 
| 43 try: | 48 try: | 
| 44 pid = int(pid_string.rstrip()) | 49 pid = int(pid_string.rstrip()) | 
| 45 if _process_running(pid): | 50 if _process_running(pid): | 
| 46 continue | 51 continue | 
| 47 except exceptions.ValueError: | 52 except exceptions.ValueError: | 
| 48 print "'%s' is not a PID." % pid_string | 53 print "'%s' is not a PID." % pid_string | 
| 49 | 54 | 
| 50 init_path = os.path.join("/etc/init.d", service) | 55 init_path = os.path.join("/etc/init.d", service) | 
| 51 if not os.path.exists(init_path): | 56 if not os.path.exists(init_path): | 
| 52 print "%s does not exist, service is not running and cannot be started." % init_path | 57 print "%s does not exist, service is not running and cannot be started." % init_path | 
| 53 continue | 58 continue | 
| 54 | 59 | 
| 55 print "%s is not running, starting ..." % service | 60 print "%s is not running, starting ..." % service | 
| 56 subprocess.call([init_path, "start"]) | 61 subprocess.call([init_path, "start"]) | 
| LEFT | RIGHT |