Created
August 16, 2012 21:17
-
-
Save Olical/3373733 to your computer and use it in GitHub Desktop.
Runs a Gimbal server and reboots it periodically: http://www.gimbalgame.com/
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
| import sys | |
| import time | |
| import msvcrt | |
| from datetime import datetime, timedelta | |
| from subprocess import Popen, PIPE | |
| # Config | |
| script_prefix = 'GIMBALSERVERMOM: ' | |
| command = ['C:\Program Files (x86)\Gimbal\GimbalServer.exe'] | |
| cwd = 'C:\Program Files (x86)\Gimbal' | |
| env_newline = '\r\n' | |
| session_length = timedelta(hours=2, seconds=10) | |
| warnings = [ | |
| timedelta(seconds=60), | |
| timedelta(seconds=30), | |
| timedelta(seconds=15), | |
| timedelta(seconds=10), | |
| ] | |
| say_command = 'say' | |
| stop_command = 'quit' | |
| def poll_process(p): | |
| # Return false if the poll failed | |
| if p.poll() != None: | |
| return False | |
| # Write the backlog of stdin into the process | |
| # Check for momentary keypress. | |
| while msvcrt.kbhit(): | |
| # Capture the keypress as one char, and send it to stdout, and to the server process's stdin. | |
| inchar = msvcrt.getch() | |
| p.stdin.write(inchar) | |
| sys.stdout.write(inchar) | |
| # Default to returning true | |
| return True | |
| def message_process(p, message): | |
| message += env_newline | |
| # Display the message | |
| sys.stdout.write(script_prefix + 'Sending Command>' + message) | |
| # Send the message to the process | |
| p.stdin.write(message) | |
| def start_loop(): | |
| # Store the boot time | |
| start_time = datetime.now() | |
| current_warning = 0 | |
| # Boot the program | |
| p = Popen(command, stdin=PIPE, stdout=sys.stdout, cwd=cwd) | |
| sys.stdout.write(script_prefix + 'Starting new instance of ' + command[0] + env_newline) | |
| # Keep looping until we pass the session length | |
| while datetime.now() < start_time + session_length: | |
| time.sleep(5) | |
| # Kick out if the process has ended | |
| if not poll_process(p): | |
| sys.stdout.write(script_prefix + 'Process ended.' + env_newline) | |
| return False | |
| # Send out the warning if required | |
| if current_warning < len(warnings) and datetime.now() > start_time + session_length - warnings[current_warning]: | |
| message_process(p, say_command + ' Server rebooting in ' + str(int(warnings[current_warning].total_seconds())) + ' seconds.') | |
| # Increment the current warning | |
| current_warning += 1 | |
| # Kill the server. | |
| message_process(p, say_command + ' The server is rebooting, please rejoin!') | |
| sys.stdout.write(script_prefix + 'Terminating process...' + env_newline) | |
| p.terminate() | |
| p.wait() | |
| sys.stdout.write(script_prefix + 'Process terminated.' + env_newline) | |
| # Return true to loop again | |
| return True | |
| # Keep running the loop while it returned true | |
| while start_loop(): | |
| pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment