5

I've setup an Ubuntu 18.04 instance, to use as a private Minecraft Bedrock server.

I've downloaded the server itself from https://www.minecraft.net/en-us/download/server/bedrock/

I can run it directly from the shell, and everything works fine. However what I'd like to do, is configure it as a service, so it will start automatically on boot, and stop gracefully etc.

The "bedrock_server" executable runs in the foreground, and waits for various commands from standard in, such as "stop", "kick", "save" etc.

Is there a way I can configure a service script so it can be controlled like so ...

$ service minecraft stop
$ service minecraft restart

It would be even better if I could issue other minecraft specific commands as well, such as...

$ service minecraft kick [player name or xuid]

etc.

I'm not too concerned about this capability though. The main thing I want, is to be able to start it properly at server boot, and gracefully stop.

I'd don't need it fully written for me. If someone could direct me to a tutorial, or a template or something, that would be great.

This is the full command I'm currently using to launch it from the shell...

$ cd /home/[my-user]/bedrock-server-1.14.60.5 && LD_LIBRARY_PATH=. ./bedrock_server

update...

Based on the answer below, and a lot of trial and error, I've come up with this systemd script...

[Unit]
Description=Minecraft Bedrock Service
After=network-online.target

[Service]
User=[my-user]
WorkingDirectory=/home/[my-user]/bedrock-server-1.14.60.5
Type=forking
ExecStart=/usr/bin/screen -dmS UbuntuMinecraft /bin/bash -c "LD_LIBRARY_PATH=. ./bedrock_server"
ExecStop=/usr/bin/screen -Rd UbuntuMinecraft -X stuff "say Stopping server in 10 seconds...\r"
ExecStop=/bin/sleep 10
ExecStop=/usr/bin/screen -Rd UbuntuMinecraft -X stuff "stop\r"
GuessMainPID=no
TimeoutStartSec=600

[Install]
WantedBy=multi-user.target

This seems to work quite well.

If I need to run other commands on the server, I can connect to the screen session like so...

$ screen -R UbuntuMinecraft

Then I can run whatever native commands I need.

I based my solution partially on this code... https://github.com/TheRemote/MinecraftBedrockServer

1 Answer 1

6

Initialisation scripts vary dependant on what version of what distribution you are using, but I believe most modern versions of Ubuntu are using systemd. So what you probably want a Systemd file.

Systemd files follow a standard format, where you mainly have to substitute the correct values in.

I haven't tested this, but following the standard syntax, you'd want something along these lines:

[Unit]
Description=Minecraft Service
After=network.target

[Service]
Type=Simple
WorkingDirectory=/home/user/bedrock-server-1.14.60.5
ExecStart=/bin/sh -c "LD_LIBRARY_PATH=. ./bedrock_server"
Restart=on-failure

[Install]
WantedBy=multi-user.target

There are different things you can configure here, such as TimeoutStopSec which lets you configure how long the service is left to shutdown when you run the 'stop' command before it kills the proccess.

If you drop the file in the right folder for your distribution (/etc/systemd/system), you will be able to control the service using the systemctl or service commands.

If you created the service with the name 'minecraft', you would be able to use commands like:

systemctl status minecraft - get the current status
systemctl starts minecraft - start the service
systemctl stop minecraft - stop the service
systemctl enable minecraft - start this service at boot

As for being able to run console commands, it depends on how the service is handled. When I used to run Java Minecraft servers you could use 'RCON' to send commands to the console, but there were other tricks, such as binding to a socket.

I cannot vouch for the safety of this code, but a quick Google, implied that someone has created the correct socket wrappers for this sort of functionality here: https://github.com/TapeWerm/MCscripts

Another option would be to start Minecraft in a screen session, which would allow you to reconnect to the running console at will.

2
  • Thanks, this helped a lot to get me started. The solution I finally found involved using the screen command to run the server. This worked quite well. May 29, 2020 at 7:48
  • You're a life saver. I had put together service files for Java edition before, but not for Bedrock edition. And for some reason every time I tried to use the command ./bedrock_server in ExecStart, it didn't work. It complained about permissions in the journalctl logs for the service, even if I added lots of permissions, changed the files to be owned by root, and made the service execute as root. Nothing helped. But your ExecStart here worked, where you use /bin/sh explicitly and have it start bedrock_server. I'm not sure why. Time for me to try to figure that out!
    – Matt Welke
    Jul 30, 2023 at 4:59

Not the answer you're looking for? Browse other questions tagged .