Controlling a minecraft server with python
os.system
will simply run the command then return to your python script with no way to further communicate with it.
On the other hand using subprocess.Popen
gives you access to the process while it runs, including writing to it's .stdin
which is how you send data to the server:
def server_command(cmd):
process.stdin.write(cmd+"\n") #just write the command to the input stream
process = None
executable = '"C:\Program Files\Java\jre1.8.0_111\bin\java.exe" -Xms4G -Xmx4G -jar craftbukkit-1.10.2.jar nogui java'
while True:
command=input()
command=command.lower()
if process is not None:
if command==("start"):
os.chdir(minecraft_dir)
process = subprocess.Popen(executable, stdin=subprocess.PIPE)
print("Server started.")
else:
server_command(command)
you can also pass stdout=subprocess.PIPE
so you can also read it's output and stderr=subprocess.PIPE
to read from it's error stream (if any)
As well instead of process.stdin.write(cmd+"\n")
you could also use the file
optional parameter of the print function, so this:
print(cmd, file=process.stdin)
Will write the data to process.stdin formatted in the same way that print normally does, like ending with newline for you unless passing end=
to override it etc.
H. Siddons
Updated on June 04, 2022Comments
-
H. Siddons almost 2 years
I've searched a lot for this and have not yet found a definitive solution. The closest thing I've found is this:
import shutil from os.path import join import os import time import sys minecraft_dir = ('server diectory') world_dir = ('server world driectory') def server_command(cmd): os.system('screen -S -X stuff "{}\015"'.format(cmd)) on = "1" while True: command=input() command=command.lower() if on == "1": if command==("start"): os.chdir(minecraft_dir) os.system('"C:\Program Files\Java\jre1.8.0_111\bin\java.exe" -Xms4G -Xmx4G -jar craftbukkit-1.10.2.jar nogui java') print("Server started.") on = "0" else: server_command(command)
When I launch this program and type 'start' the CMD flashes up and closes instantly. Instead I want the CMD to stay open with the minecraft sever running from it. I'm not sure why this happens or what the problem is, any help would be greatly appreciated.
p.s. I have edited this to my needs (such as removing a backup script that was unnecessary) but it didn't work before. The original link is: https://github.com/tschuy/minecraft-server-control