binhex / arch-minecraftserver

Docker build script for Arch Linux base with Minecraft Java server
GNU General Public License v3.0
18 stars 27 forks source link

Server is not restarted after crash #8

Open bo0tzz opened 3 years ago

bo0tzz commented 3 years ago

If the minecraft server process crashes, a manual restart of the container is required to get it back in working order. I would expect that either the minecraft server process is restarted automatically inside the container, or that the entire container goes down (so that it can be restarted by an orchestrator).

szaimen commented 1 year ago

Hi, see https://github.com/binhex/arch-minecraftserver/pull/14 for a poc

awnumar commented 1 month ago

Another possibility could be adding something to the screen that contains the minecraft process like:

for (( ; ; ))
do
        java -Xmx7G -jar minecraft_server.jar nogui
        echo "Server closed unexpectedly, restarting in 10 seconds..."
        sleep 10
done
binhex commented 1 month ago

ive put in a very basic infinite while loop so the server process will now restart on crash, please pull down the latest image and let me know how it goes.

awnumar commented 1 month ago

Thanks for adding that @binhex

I've tested that by pulling the latest image and:

each time the server automatically restarted so it seems like this patch works! Thanks.

awnumar commented 1 month ago

I have just realised that this patch unintentionally makes #12 a little worse to deal with. Whenever I want to gracefully shut down, I login to the web-ui and run stop, or run that in-game, wait for it to finish gracefully shutting down and then stop the container.

Now however because it automatically restarts I'm worried there's no way to gracefully shut down as the server will auto-restart before you can stop the container 🤔

I'm not sure if there is a good solution to both problems at the same time. One approach is to run the save command before uncleanly exiting but it's not perfect.

awnumar commented 1 month ago

Maybe something like:

binhex commented 1 month ago

Whenever I want to gracefully shut down, I login to the web-ui and run stop, or run that in-game, wait for it to finish gracefully shutting down and then stop the container.

I shall put in additional code to trap SIGTERM and CTRL+C, this should then handle the case where you want to force a shutdown whilst still permitting the server to restart.

This is a different problem and has already been addressed, i am making use of dumb-init to pass signals along, i also have written a script to wait for the process to end which i will include to try and ensure the process is not sent a SIGKILL which should fix https://github.com/binhex/arch-minecraftserver/issues/12, it is a little tricky as there is a lot of wheels in wheels going on here.

awnumar commented 1 month ago

Thanks, sounds good. I'm happy to test the changes when you need.

binhex commented 1 month ago

the changes are in, please pull down latest.

awnumar commented 3 weeks ago

@binhex I've pulled the latest image and tried to trigger a graceful shutdown, sadly I wasn't able to. Testing on Unraid.

When I stop the container, the webUI immediately loses connection and the server stops immediately. I've checked the latest.log and the screen.log files and neither have any indication of the server gracefully stopping. Additionally, when logged into the server as a player, I get the error message "Disconnected - Connection lost" instead of "Disconnected - Server closed".

If I shell into the container and do a kill on the pid of the java process, I do get the expected "Server closed" message and the latest.logs file shows the expected server shutting down log lines.

awnumar commented 2 weeks ago

Perhaps instead of running the java process inside a screen session, the infinite loop and the java command can run in a blocking fashion at the end of the start.sh script? I think because the current signal catching logic is inside the screen session it may not be receiving any signal, or the pid1 process is exiting immediately and not waiting for the screen to exit. It might be simpler if the subprocess isn't inside a screen, though that might break the way the webui works.

Maybe also the root of the start.sh script could catch the signals and explicitly pkill java and wait for it.

I recently saw this article which has some interesting information: https://sirikon.me/posts/0009-pid-1-bash-script-docker-container.html

awnumar commented 2 weeks ago

Potentially more powerful version of dumb-init which supports multiple children: https://github.com/linkdd/procfusion