1

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?

andrewJames
  • 19,570
  • 8
  • 19
  • 51
juleklO
  • 31
  • 6
  • 2
    What terminals do `ps` say the old and new processes are connected to? – Joseph Sible-Reinstate Monica May 31 '22 at 00:13
  • @JosephSible-ReinstateMonica the process does not appear after using ``` ps ``` I've originally found it through my conky monitor, and than again through the system monitor launched from the task bar. ``` ps -h ``` *Cont. in next comment.* – juleklO May 31 '22 at 01:34
  • Could you use nohup? And then tail -f nohup.out to monitor the server logs? – HSchmale May 31 '22 at 01:36
  • @JosephSible-ReinstateMonica ``` ps -h ``` outputs: ``` 2172 tty2 Ssl+ 0:00 /usr/lib/gdm3/gdm-x-session --run-script env GNOME_ 2174 tty2 Sl+ 0:56 /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user 2187 tty2 Sl+ 0:00 /usr/libexec/gnome-session-binary --systemd --syste 2888 pts/0 Ss+ 0:00 bash 5700 pts/0 S 0:00 sh ./restart.sh 5703 pts/0 Sl 2:44 java -Xmx16384M -Xms16384M -jar paper-1.18.1-108.ja 5779 pts/1 Ss 0:00 bash 6111 pts/1 R+ 0:00 ps -h ``` – juleklO May 31 '22 at 01:38
  • 1
    @HSchmale that might work for reading the output, but still doesn't help the fact I can't interact with the terminal/console from the machine Still - I will the the nohup tip - just tomorrow as it is 4:30am here and I'm heading to bed – juleklO May 31 '22 at 02:28
  • 1
    The best practice is generally to not care about being attached to a console. Your logs should be shipped somewhere and you should be receiving alerts if your service needs to be bounced. – HSchmale May 31 '22 at 02:43
  • @MartinZeitler the script doesn't run constantly, it's only activated by a pre-made plugin at specified times – juleklO May 31 '22 at 12:31
  • @MartinZeitler about running the process as a service, so far the method resulted in the same 'disappearing' of the process. It still runs, but I can't interact with it. *Unless I'm doing something wrong, which is very plausible* – juleklO May 31 '22 at 12:35
  • I think it does run constantly since there is a `while true` and nothing killing it, it will not stop trying to boot the server until it is killed. By that I mean even after successfully starting the server it will try again 30 seconds later. – cfgn May 31 '22 at 13:50
  • Does this answer your question: [What are the -Xms and -Xmx parameters when starting JVM?](https://stackoverflow.com/questions/14763079/what-are-the-xms-and-xmx-parameters-when-starting-jvm) – Martin Zeitler May 31 '22 at 17:27
  • @juleklO run it manually once, in order to determine why it even exits; the local firewall might prevent access to the web-console - or possibly the configured port is already used by some other service. Unless you'd manage to produce some kind of error log, this all is just guessing around. Try something alike `-Xms1G -Xmx16G` (in other words: starting with `1G`, permitted to use up to `16G`). – Martin Zeitler May 31 '22 at 17:45

2 Answers2

1

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.

cfgn
  • 201
  • 2
  • 10
  • Hey there, I had try with the script, first time off, it output this: https://ibb.co/0992Cw6 Another tries resulted in the process just terminating like shown here: https://ibb.co/ZfXzpBW If I'm doing something wrong please correct me. https://ibb.co/KFJwJ2k – juleklO May 31 '22 at 12:30
  • The first screen is strange since you are not supposed to get the server output from the deamonized screen command... I'll look into what could have happened. For the issue with awk did you already install gawk? If not you can do it with `sudo apt-get install gawk`. Does `screen -ls` return any process? – cfgn May 31 '22 at 12:44
  • I modified the script to change how was handled the wait. Does it work on your side? – cfgn May 31 '22 at 13:05
  • To start off, I've installed gawk, below in order I'll attach screenshots of steps showing the starting/rebooting the process (with the updated script): https://ibb.co/0hRF6p8 - starting screen session https://ibb.co/ZHbm5Zc - starting the server inside the screen session https://ibb.co/Tw5tc1k - running the reboot script on a live server https://ibb.co/QF3CGw9 - the server has stopped, but not rebooted, the screen session is still up and running – juleklO May 31 '22 at 13:16
  • I may be wrong but from the screens it seems like you are not using screen as I originally though. You should first kill the screen you are attached to (`exit` will do), then use the command `screen -dmS java -Xmmx16384M -Xms16384M -jar /paper-1.18.1-108.jar nogui` to start the server. If this is what you already did, sorry, I may have misunderstood. This could be the issue since my solution is build around a daemonized screen running a single command. – cfgn May 31 '22 at 13:24
  • I tried running it as you described, below are the results, running the command from outside the screen does not seem to affect the screen session https://ibb.co/r50Gpqb https://ibb.co/QCK6gN2 https://ibb.co/jy6vqLx – juleklO May 31 '22 at 14:00
  • No `minecraft` screen should be running before starting the server, it will begin with the command. You can use `screen -r minecraft` then `exit` to kill the ones you see in `screen -ls`. By the way I can confirm the script works on Ubuntu Server from my test, I tested the crontab setup too just in case. – cfgn May 31 '22 at 14:04
  • I made sure no screen sessions are active, I ran the command, the terminal skipped to allow me to input the next command, but nothing beyond that. https://ibb.co/6NkHRr5 *again I might be making some absolute newbie mistakes here, so please excuse that* – juleklO May 31 '22 at 14:41
  • You are doing it fine as far as I can see, since the screen did not start could you try to execute the command following alone, see which error is displayed? – cfgn May 31 '22 at 16:11
  • I'm not sure what exactly I should do, could you please rephrase? – juleklO May 31 '22 at 22:18
  • Since the screen does not persist, it means the following command exits, you may want to type it in the Server directory since Minecraft servers check if the local eula.txt is set to true. – cfgn Jun 01 '22 at 05:03
  • Another option would be to launch a start.sh with cd to the server's folder and use `screen -dmS minecraft sh /start.sh`. The way to see which issue the command have is to launch it separately for example `java -Xmmx16384M -Xms16384M -jar /paper-1.18.1-108.jar nogui` from an unrelated directory causes eula.txt not existing in current directory and crashes the server. – cfgn Jun 01 '22 at 05:11
  • Alright after some testing this command launched the server in a detached screen session: `screen -dmS minecraft sh /media/julian/Server/Server/start.sh`. I tried running the reboot.sh from the original answer, it closed the screen session, but it didn't shut down the process/server properly, and it did not re-launch, hope this helps you help, of course if you still have the willpower to deal with me :P – juleklO Jun 01 '22 at 14:11
  • I can't really reproduce the issue on my side, despite having tried on multiple servers (even modded ones!). On my side, the script waits for about one second after the stop command was sent... I'm not sure what might cause the screen's child process to stay alive. I presume you have already tried after a fresh server reboot, maybe the fact I'm using Ubuntu Server instead of Ubuntu changes something beyond my understanding. – cfgn Jun 01 '22 at 21:06
1

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!

juleklO
  • 31
  • 6