Home > Net >  Ghost Java process after restarting a game server
Ghost Java process after restarting a game server

Time:06-03

I run a small private server on a ubuntu machine. It's on 24/7 for me and a couple of my friends, but I also use it to learn a bit about managing such gaming servers.

I want to restart it daily to free up some resources and let the process reset. I use a plugin for running a command or/and a script for restarting the process. The plugin stops the current process and runs the script below to restart it.

#!/bin/sh
while true
do
java -Xmmx16384M -Xms16384M -jar paper-1.18.1-108.jar nogui
sleep 30
done

The process stops just fine, the problem is the new process launched by the script doesn't run in the same terminal. System monitor indicated the process running, and sure enaugh you can join the server just fine. The problem occurs when I want to use the server terminal, as it is nowhere to be found. Using screen doesn't help either, the screen session stops where the original java stopped, yet the process starts to live somewhere else.

My question would be is there any way to 'tap into' the new process, to be able to input commands into it.

Or maybe I'm doing somthing wroung, and I should school myself on similar things, if so please could you provide me with resources?

CodePudding user response:

Addressing orphaned processes

For any totally detached process running in the background you can use kill <pid> where <pid> is the PID of the relevant process. If you are not sure what to kill, just reboot.

You would ideally want a single script like reboot.sh.

Reboot script

First off, it should stop the server. Then, you'll want to wait for it to be down, using its PID. Finally, you can start it again.

Here is a script (from memory) for this:

#!/bin/sh
# Needed to allow for relative paths.
cd <pathToServerFiles>
# Send the stop command to the server.
screen -S <nameOfScreen> -p 0 -X stuff "stop^M" # '^M' stands for ENTER key.
# Wait for the server to stop properly.
while screen -ls | grep -q <nameOfScreen>
do
    sleep 1
done
# Start a new screen executing the java command on the server JAR.
screen -dmS <nameOfScreen> java -Xmmx16384M -Xms16384M -jar paper-1.18.1-108.jar nogui

Schedule tasks

Then you can use crontab -e (included by default on Ubuntu) to setup a daily task, for example thereafter executing the script daily at 3 AM:

0 3 * * * <pathToServerFiles>/restart.sh

In order to handle hardware restarts (without a doubt necessary for updates) you can even add to your crontab:

@reboot screen -dmS <nameOfScreen> java -Xmmx16384M -Xms16384M -jar <pathToServerFiles>/paper-1.18.1-108.jar nogui

Using screen basic commands

You can then start your server with the command:

screen -dmS <nameOfScreen> java -Xmmx16384M -Xms16384M -jar <pathToServerFiles>/paper-1.18.1-108.jar nogui

Look at your logs with:

screen -r <nameOfScreen>

(Use Ctrl A then Ctrl D to detach from screen without killing it)

Check which screens are running with:

screen -ls

Logging

As far as I know, Minecraft servers have a log folder under which you can find the console output duplicated in real time, so this should not need a separate option on screen (it exists, but having two similar log files doesn't seem to be what you aim to achieve).

Note

Every string balised with < and > should be changed to the appropriate value, based on your context.

Let me know if this helped you and if you need details/changes.

CodePudding user response:

Conclusion

Alright for anyone who might experience similar issues, here's the solution I've managed to work out. Huge thanks to @cfgn, without his help I wouldn't be able to come to any conclusion.

Prerequisites

  • Make sure your drive is mounted, you can mount drives automatically on system startup as shown here.
  • Make sure screen is installed, a screen tutorial can be found here.
  • Make sure all .sh files you create are 'executable'.

Startup

This is a script I use to get the server going for the first time after launch. It executes the java -Xmx14336M -Xms14336M -jar paper-1.18.1-108.jar nogui command inside a new screen called 'minecraft', then detaches from said screen. If you use screen -r minecraft you'll be able to see and interact with the console.

#!/bin/sh
#launches the server process in a detached screen session
screen -dmS minecraft java -Xmx14336M -Xms14336M -jar paper-1.18.1-108.jar nogui

Restarting

This is the restarting script itself, all comments inside it explain pretty well what each line does. All in all you'll see the server stop, when it is done closing the screen session will close (sometimes these sessions become 'dead' this is what the last screen -wipe line is for). After 10 seconds, a new screen session with the same name (but most likely a different PID) will be launched with the same parameters.

#!/bin/sh
#stops the server via the /stop command
screen -dmS minecraft -p 0 -X stuff stop^M
#waits 10 seconds for the server to close and -cool down-
sleep 10
#launches the server again
screen -dmS minecraft java -Xmx14336M -Xms14336M -jar paper-1.18.1-108.jar nogui
#wipes dead screens if such get left over
screen -wipe

Last thoughts

  • It is important to name the files something so can identify easily.
  • If you're using spigot (and if you are here most likely you are) you need to update the spigot.yml to reflect the changes. Under settings the restart script should look like restart-script: ./restart.sh if your script is named 'restart.sh'.

Again huge thanks to cfgn and all other people for taking their time to help out!

  • Related