-
Notifications
You must be signed in to change notification settings - Fork 16
/
auto_restart_apache.py
executable file
·49 lines (43 loc) · 1.62 KB
/
auto_restart_apache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# Copyright (C) 2020 The Dofus Fashionista
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import datetime
import socket
import urllib2
from subprocess import call
def is_localhost_down():
try:
home_page = urllib2.urlopen('http://dofusfashionista.com', timeout=120)
first_bytes = home_page.read(200)
if first_bytes:
# print_with_time('Home page ok.')
return False
else:
print_with_time('Empty response.')
return True
except (urllib2.URLError, socket.timeout, socket.error) as e:
print_with_time(str(e))
return True
def main():
is_down = is_localhost_down()
if is_down:
print_with_time('Restarting apache because server is down.')
call(['/etc/init.d/apache2', 'restart'])
def print_with_time(s):
print '[%s]' % datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
print s
if __name__ == '__main__':
main()