Scripting the Minecraft Server Wrapper - Gamocosm/Gamocosm GitHub Wiki
The Minecraft Server Wrapper wraps the Minecraft server process and behaves like a proper server daemon. It also pipes the server process' standard input and provides an HTTP API to communicate with the server. You can view the source and README at its repository.
Basics
The three endpoints you probably care most about are "start", "stop", and "exec". The source is well documented and it's best you check that for details/other endpoints.
# get the second line (password) from the auth file
MCSW_PASSWORD=$(sed -n '2{p;q}' /opt/gamocosm/mcsw-auth.txt)
# starting server with 1024MB (1GB) of RAM
curl --data '{ "ram": "1024M" }' "http://gamocosm-mothership:$MCSW_PASSWORD@localhost:5000/start"
# stopping the server
curl "http://gamocosm-mothership:$MCSW_PASSWORD@localhost:5000/stop"
# sending a command to the server
curl --data '{ "command": "say hi" }' "http://gamocosm-mothership:$MCSW_PASSWORD@localhost:5000/exec"
As you can see, the only thing that really changes is the --data
flag, and the endpoint after the slash at the end of the URL.
If you try to start a server that's already running it will return the pid of the existing process.
Likewise, if you try to stop a server that's already stopped it will return "successful".
You don't have to create a $MCSW_PASSWORD
variable; you can simple replace it with an inline $(sed -n '2{p;q}' /opt/gamocosm/mcsw-auth.txt)
, or check the value yourself in the auth file, and hardcode it.
Client
There is a client in MCSW repo that simplifies doing these long curl
s (it just does what's above).
It's pretty simple to use:
/opt/gamocosm/bin/mcsw-client start '{ "ram": "1024M" }'
/opt/gamocosm/bin/mcsw-client stop
/opt/gamocosm/bin/mcsw-client exec '{ "command": "say hi" }'
If you don't want to type the whole path every time, you can add it onto your $PATH
environment variable.
Add PATH="$PATH:/opt/gamocosm/bin"
to your ~/.bash_profile
(will be available whenever you SSH/login to your server) or whatever script you're writing (will be available locally).
Then you can just do mcsw-client ...
.
Example script
Here is a script that will do a backup of a server.
#!/bin/bash
PATH="$PATH:/opt/gamocosm/bin"
mcsw-client exec '{ "command": "save-all" }'
cd "$HOME/minecraft"
mkdir -p backups
# timestamp in year_month_day-hour:minute format
TIMESTAMP="$(date +'%Y_%m_%d-%H:%M')"
tar -czf "backups/world-$TIMESTAMP.tar.gz" world