Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/management/bin/start_services.py

Issue 8963083: sitescripts: Script to keep services alive (Closed)
Patch Set: Created Nov. 30, 2012, 1:32 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2012 Eyeo GmbH
5 #
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
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
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/>.
17
18 import os, subprocess
19 from sitescripts.utils import get_config
20
21 def _get_services():
22 config = get_config()
23 section_name = "keep_alive_services"
24 section_keys = config.options(section_name)
25 default_keys = config.defaults().keys()
26 keys = set(section_keys) - set(default_keys)
27
28 services = {}
29 for key in keys:
30 services[key] = config.get(section_name, key)
31 return services
32
33 def _process_running(pid):
34 try:
35 os.kill(pid, 0)
36 return True
37 except OSError:
38 return False
39
40 if __name__ == "__main__":
41 services = _get_services()
42 for service in services.keys():
43 pid_path = os.path.join("/var/run", services[service])
44 if os.path.exists(pid_path):
45 with open(pid_path) as file:
46 pid_string = file.read()
47
48 try:
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
54
55 init_path = os.path.join("/etc/init.d", service)
56 if not os.path.exists(init_path):
57 print "%s does not exist, service is not running and cannot be started." % init_path
58 continue
59
60 print "%s is not running, starting ..." % service
61 subprocess.call([init_path, "start"])
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld