Add script to run a Minecraft server in the background

This commit is contained in:
Max von Forell 2019-10-03 16:06:51 +02:00
parent 42f18a2d4a
commit ea6d078145
4 changed files with 55 additions and 0 deletions

View File

@ -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.

View File

@ -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()

View File

@ -0,0 +1,3 @@
#!/bin/sh
java -Xmx2048M -Xms2048M -jar server.jar -nogui

View File

@ -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 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)| |[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)| |[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]()| |[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)| |[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)| |[Python Algebra Solver](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Algebra-Solver)|[Sengxay Xayachack](https://github.com/frankxayachack)|