Skip to content

Instantly share code, notes, and snippets.

@ngld
Created March 17, 2018 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngld/4934c1c49773c2b639fe71218d352941 to your computer and use it in GitHub Desktop.
Save ngld/4934c1c49773c2b639fe71218d352941 to your computer and use it in GitHub Desktop.
Short script used to restart FS2's standalone server.
# Usage:
# Save this file as restarter.py in the same directory as the fs2 executable, install Flask and run "FLASK_APP=restarter.py flask run -p 8080 --no-reload --without-threads".
# Afterwards, either open the port or proxy it through a web server (like nginx) to make the web interface accessible.
# The script will write the standalone's stdout and stderr to out.log; make sure it has write permissions for that file.
import subprocess
from time import sleep
from threading import Thread
from flask import Flask, redirect
application = app = Flask(__name__)
p = None
log = open('out.log', 'w+')
class Watcher(Thread):
def __init__(self):
super().__init__()
self.daemon = True
self.start()
def run(self):
global p
while True:
print('Starting standalone...')
p = subprocess.Popen(['./fs2', '-standalone'], stdin=subprocess.DEVNULL, stdout=log, stderr=subprocess.STDOUT)
p.wait()
print('Server exited with code %d.' % p.returncode)
p = None
sleep(3)
w = Watcher()
@app.route('/')
def index():
html = '<!DOCTPYE html><html><head>'
html += '<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">'
html += '</head><body><div class="container">'
html += '<h1>FS2 Standalone Restarter</h1>'
if p:
html += '<p>The server is running.</p>'
else:
html += '<p>The server is <strong>NOT</strong> running.</p>'
html += '<p><a href="/restart">Restart !!!!</a></p>'
log.seek(0)
out = log.read()
html += '<p><pre>%s</pre></p>' % out.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
html += '</div></body></html>'
return html
@app.route('/restart')
def restart_stuff():
global p
if p:
print('Killing server!')
p.kill()
return redirect('/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment