Minecraft server unsafe restart

I wrote a simple systemd service to start minecraft, but on stop I believe it does a unsafe kill.

{pkgs, ... }: {
        systemd.services.name = {
                enable = true;
                unitConfig = {
                        Description = "Name's minecraft server";
                        Wants = "network-online.target";
                        After = "network-online.target";
                };

                serviceConfig = {
                        Type = "exec";
                        User = "redhawk";
                        WorkingDirectory = "/home/redhawk/minecraft/name/";
                        ExecStart = "${pkgs.jre}/bin/java -jar server.jar";
                        KillSignal= "SIGTERM";
                };

                wantedBy = [ "multi-user.target" ];
        };
}

Also how do i ssh into a service?

the logs

Dec 01 03:52:47 Paisley-Park java[1289448]: [03:52:47] [Server thread/INFO]: Done (5.117s)! For help, type "help"
Dec 01 03:52:51 Paisley-Park systemd[1]: Stopping Name's minecraft server...
Dec 01 03:52:52 Paisley-Park systemd[1]: Name.service: Main process exited, code=exited, status=143/n/a
Dec 01 03:52:52 Paisley-Park systemd[1]: Name.service: Failed with result 'exit-code'.
Dec 01 03:52:52 Paisley-Park systemd[1]: Stopped Name's minecraft server.
Dec 01 03:52:52 Paisley-Park systemd[1]: Name.service: Consumed 1min 4.596s CPU time, read 0B from disk, written 292.0K to disk, no IP traffic.
Dec 01 03:52:52 Paisley-Park systemd[1]: Starting Name's minecraft server...
Dec 01 03:52:52 Paisley-Park systemd[1]: Started Name's minecraft server.
Dec 01 03:52:52 Paisley-Park java[1289643]: Starting net.minecraft.server.Main

This isn’t how it should look if the server is being save on shutdown.

I don’t have a lot of experience with Minecraft servers but I’ve seen 2 different approaches for gracefully stopping a server:

  • Write “stop” into the stdin of the server process, in this case by binding it to a socket (this is what the minecraft server module does, have a look at the stopScript).
  • Using the mcrcon package to stop the server. This might require some additional settings to enable rcon for the server but I’m not sure. This can be seen in this module for running modded minecraft servers.

Unless you have a good reason not to, I’d recommend to just use the services.minecraft-server option and save yourself the trouble though.

I’m not entirely sure what you mean by “ssh-ing into the service”. The service is just a process running on your machine, so (if you have the correct permissions) you can directly access all the files of your service.

So when you run the stop command on the server it prints out saving message, but when given a SIGTERM signal it still does the same, It just doesn’t look like it.