From ea6d078145b17f6caef87957ebde8268558b151b Mon Sep 17 00:00:00 2001 From: Max von Forell Date: Thu, 3 Oct 2019 16:06:51 +0200 Subject: [PATCH] Add script to run a Minecraft server in the background --- Minecraft_server_in_background/README.md | 22 ++++++++++++++++++ Minecraft_server_in_background/run.py | 29 ++++++++++++++++++++++++ Minecraft_server_in_background/start.sh | 3 +++ README.md | 1 + 4 files changed, 55 insertions(+) create mode 100644 Minecraft_server_in_background/README.md create mode 100644 Minecraft_server_in_background/run.py create mode 100644 Minecraft_server_in_background/start.sh diff --git a/Minecraft_server_in_background/README.md b/Minecraft_server_in_background/README.md new file mode 100644 index 0000000..3011a56 --- /dev/null +++ b/Minecraft_server_in_background/README.md @@ -0,0 +1,22 @@ +# Running a Minecraft server in the background + +This program runs a script (which can be specified) in a subprocess with redirected output +(new output location can be specified) and periodically checks a file for a keyword (both +the name of the file to check and the keyword to check for can be specified) +and exits (stopping the subprocess via sending a command), if the contents of the file +include the keyword. + +You probably want to run this script in background, e.g. calling it via './run.py &' +or via 'nohup ./run.py &'. + +A sample invocation could look like this: + +```bash +nohup ./run.py & +``` +Now the specified script, e.g. a Minecraft server, is running in the background. + +```bash +echo stop > command.txt +``` +After a short delay, the script in the background will be stopped. diff --git a/Minecraft_server_in_background/run.py b/Minecraft_server_in_background/run.py new file mode 100644 index 0000000..988e2e1 --- /dev/null +++ b/Minecraft_server_in_background/run.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import subprocess +import time + +filename_script = './start.sh' # the script that will be executed +filename_script_output = './log.txt' +filename_own_input = 'command.txt' # the file this script periodically reads from +stop_command = b'stop\n' # must be a binary string +exit_keyword = 'stop' + +with open(filename_own_input, 'w') as f: + f.write('') # reset content of file and create it if needed + +fd_script_output = open(filename_script_output, 'w') # create file descriptor for script to write its stdout to +script_process = subprocess.Popen( # start new process running script + filename_script, + stdin=subprocess.PIPE, # needed for script_process.communicate() (see below) + stdout=fd_script_output # redirect output +) + +while True: + with open(filename_own_input, 'r') as f: + if exit_keyword in f.read(): # check if we should exit + script_process.communicate(input=stop_command) # stop subprocess and wait for it to terminate + break + time.sleep(1) + +fd_script_output.close() diff --git a/Minecraft_server_in_background/start.sh b/Minecraft_server_in_background/start.sh new file mode 100644 index 0000000..3b5b50e --- /dev/null +++ b/Minecraft_server_in_background/start.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +java -Xmx2048M -Xms2048M -jar server.jar -nogui diff --git a/README.md b/README.md index 89b4a09..8edcfe2 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ So far, the following projects have been integrated to this repo: |[Image circle formatter](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Image-Circulator) |[Berk Gureken](https://github.com/bureken) | |[Image To PDF](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/images2pdf)|[msaoudallah](https://github.com/msaoudallah)| |[Instadp Web Scrapper](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/InstadpShower)|[Psychiquest](https://github.com/psychiquest)| +|[Minecraft Server in background](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Minecraft_server_in_background)|[Max von Forell](https://github.com/mvforell)| |[Own IP locator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Location_Of_Own_IP_Adress)|[Chris]()| |[Port Scanner](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Port_Scanner)|[Plutoberth](https://github.com/Plutoberth)| |[Python Algebra Solver](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Algebra-Solver)|[Sengxay Xayachack](https://github.com/frankxayachack)|