Last active
August 8, 2016 11:22
-
-
Save RANUX/e9dd45b187d9de9e8af9b3da305d4bae to your computer and use it in GitHub Desktop.
Restart nodebb if 504 bad gateway error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import requests | |
import subprocess | |
import time, signal, logging | |
import os | |
logging.basicConfig(level=logging.INFO) | |
cur_dir = os.path.dirname(os.path.abspath(__file__)) | |
nodebb_path = os.path.join(cur_dir, 'nodebb') | |
def check_forum_up(): | |
r = requests.get('http://nodebb.somehost.com') | |
if r.status_code == 504: | |
logging.warning('NodeBB restarted!') | |
subprocess.check_call([nodebb_path, 'reload']) | |
elif r.status_code == 502: | |
subprocess.check_call([nodebb_path, 'start']) | |
logging.warning('NodeBB started!') | |
else: | |
logging.info('NodeBB works fine') | |
def sigterm_handler(signum, frame): | |
logging.warning('Shut down restart502.py.') | |
exit(0) | |
if __name__ == "__main__": | |
logging.info('Script started!') | |
signal.signal(signal.SIGTERM, sigterm_handler) | |
while True: | |
check_forum_up() | |
time.sleep(5*60) # check every 5 minutes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample supervisor config to run restart-nodebb script:
[program:restartnodebb]
command=sudo -i /bin/bash -c -i "cd /home/app/nodebb && /home/app/nodebb/restart-nodebb.py"
directory=/home/app/nodebb
user=root
redirect_stderr=true
autostart=true
stdout_logfile=/home/app/nodebb/restart.log
stdout_logfile_maxbytes=1MB
autorestart=true
priority=10