Skip to content

Commit ea6d078

Browse files
committed
Add script to run a Minecraft server in the background
1 parent 42f18a2 commit ea6d078

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Running a Minecraft server in the background
2+
3+
This program runs a script (which can be specified) in a subprocess with redirected output
4+
(new output location can be specified) and periodically checks a file for a keyword (both
5+
the name of the file to check and the keyword to check for can be specified)
6+
and exits (stopping the subprocess via sending a command), if the contents of the file
7+
include the keyword.
8+
9+
You probably want to run this script in background, e.g. calling it via './run.py &'
10+
or via 'nohup ./run.py &'.
11+
12+
A sample invocation could look like this:
13+
14+
```bash
15+
nohup ./run.py &
16+
```
17+
Now the specified script, e.g. a Minecraft server, is running in the background.
18+
19+
```bash
20+
echo stop > command.txt
21+
```
22+
After a short delay, the script in the background will be stopped.

Minecraft_server_in_background/run.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python3
2+
3+
import subprocess
4+
import time
5+
6+
filename_script = './start.sh' # the script that will be executed
7+
filename_script_output = './log.txt'
8+
filename_own_input = 'command.txt' # the file this script periodically reads from
9+
stop_command = b'stop\n' # must be a binary string
10+
exit_keyword = 'stop'
11+
12+
with open(filename_own_input, 'w') as f:
13+
f.write('') # reset content of file and create it if needed
14+
15+
fd_script_output = open(filename_script_output, 'w') # create file descriptor for script to write its stdout to
16+
script_process = subprocess.Popen( # start new process running script
17+
filename_script,
18+
stdin=subprocess.PIPE, # needed for script_process.communicate() (see below)
19+
stdout=fd_script_output # redirect output
20+
)
21+
22+
while True:
23+
with open(filename_own_input, 'r') as f:
24+
if exit_keyword in f.read(): # check if we should exit
25+
script_process.communicate(input=stop_command) # stop subprocess and wait for it to terminate
26+
break
27+
time.sleep(1)
28+
29+
fd_script_output.close()
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
java -Xmx2048M -Xms2048M -jar server.jar -nogui

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ So far, the following projects have been integrated to this repo:
3434
|[Image circle formatter](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Image-Circulator) |[Berk Gureken](https://github.com/bureken) |
3535
|[Image To PDF](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/images2pdf)|[msaoudallah](https://github.com/msaoudallah)|
3636
|[Instadp Web Scrapper](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/InstadpShower)|[Psychiquest](https://github.com/psychiquest)|
37+
|[Minecraft Server in background](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Minecraft_server_in_background)|[Max von Forell](https://github.com/mvforell)|
3738
|[Own IP locator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Location_Of_Own_IP_Adress)|[Chris]()|
3839
|[Port Scanner](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Port_Scanner)|[Plutoberth](https://github.com/Plutoberth)|
3940
|[Python Algebra Solver](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Algebra-Solver)|[Sengxay Xayachack](https://github.com/frankxayachack)|

0 commit comments

Comments
 (0)