aboutsummaryrefslogtreecommitdiff
path: root/mcwrapper
diff options
context:
space:
mode:
authorKarel Kočí <cynerd@email.cz>2015-12-11 18:51:23 +0100
committerKarel Kočí <cynerd@email.cz>2015-12-11 18:51:23 +0100
commitd3bcf2e4366093049b5b372fbdbe14f902ae35be (patch)
tree8c20530d5e70d20a97ec12b11faedea00f6a1e69 /mcwrapper
parentb965c6e8d9ac5009a7a7f4c647e092289618e634 (diff)
downloadmcserver-wrapper-d3bcf2e4366093049b5b372fbdbe14f902ae35be.tar.gz
mcserver-wrapper-d3bcf2e4366093049b5b372fbdbe14f902ae35be.tar.bz2
mcserver-wrapper-d3bcf2e4366093049b5b372fbdbe14f902ae35be.zip
Add automatic server shutdown
Wrapper can be set to stop server if no player is online for certain time.
Diffstat (limited to 'mcwrapper')
-rwxr-xr-xmcwrapper29
1 files changed, 29 insertions, 0 deletions
diff --git a/mcwrapper b/mcwrapper
index b260d6f..f758707 100755
--- a/mcwrapper
+++ b/mcwrapper
@@ -9,6 +9,7 @@ import datetime
import traceback
import atexit
from threading import Thread
+from threading import Timer
import importlib.machinery as imp
#################################################################################
# Load configuration
@@ -50,6 +51,30 @@ if not 'verbose_level' in vars(conf):
conf.verbose_level = 0
if not 'server' in vars(conf):
conf.server = dict()
+if not 'timeout' in vars(conf):
+ conf.timeout = 0
+
+#################################################################################
+
+def autoshutdown_enable():
+ global shutdownTimeout
+ if (conf.timeout > 0):
+ if (conf.verbose_level > 0):
+ print("Automatic shutdown after " + str(conf.timeout) +
+ " min.")
+ shutdownTimeout = Timer(conf.timeout * 60.0, __server_send_stop__)
+ shutdownTimeout.start();
+ pass
+
+def autoshutdown_disable():
+ global shutdownTimeout
+ try:
+ shutdownTimeout.cancel()
+ del shutdownTimeout
+ if (conf.verbose_level > 0):
+ print("Automatic shutdown disabled.")
+ except NameError:
+ pass
#################################################################################
@@ -70,6 +95,7 @@ def __user_join__(username):
with open(playersFile, 'a') as f:
players.add(username)
f.write(username + '\n')
+ autoshutdown_disable()
def __user_leave__(username):
global playerCount
@@ -83,6 +109,8 @@ def __user_leave__(username):
f.writelines(players)
if players:
f.write('\n')
+ if (not players):
+ autoshutdown_enable()
def __server_start__():
if conf.verbose_level >= 0:
@@ -130,6 +158,7 @@ def __parse_line__(line):
print("Server start.")
with open(statusFile, 'w') as f:
f.write(__STATUSSTRINGS__[2] + '\n')
+ autoshutdown_enable()
elif ': Stopping the server' in line:
print("Server stop.")
with open(statusFile, 'w') as f: