Issue
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?
Solution
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 likerestart-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!
Answered By - juleklO Answer Checked By - Gilberto Lyons (WPSolving Admin)