@Isaac Great I was able to help you! It makes my day a bit better.
I added your findings to my post.
Have a nice day.
My issue is that a server without any players consume around 25% CPU (from probably one core).
This is a screenshot from my kodi mediacenter which also acts as minecraft server for my sons and their friends.
Servers are only accessed a few times a week but I want to have them available all the time.
The machine is not a very powerful machine, but all other (idle) processes use a lot less CPU.
Linux kodi 5.3.0-53-generic #47~18.04.1-Ubuntu SMP
memory 15GiB System memory
processor Intel(R) Celeron(R) CPU N3150 @ 1.60GHz
Do not worry.
Nobody cares about this issue which is still present on 1.15.2 servers as well.
The bug is "resolved" but isn't in reality.
Reminds me of my corporate days when helpdesk people were paid for every ticket they were able to close.
Tickets were closed without solving the problem but they reached their target.
My 3 java servers are all still running 25% CPU on idle.
It sucks but CPU time is probably cheaper than the time needed to fix this issue.
Yes, it has never been below 25% CPU usage on an empty server without players.
But don't worry, this issue is eternally resolved, so have a nice day and don't worry about your CPU usage.
So I have 3 servers running for my kids which are barely used.
My minecraft server now acts more like a electrical heat source, not a huge problem now winter is coming.
Anyway, I thought about just putting the java process into sleep mode when there are no users connected and waking it up when a user tries to connect. It could actually be used for other server client applications as well.
The method works but it's rather dirty (not quick, because it took me around 20 hours to find out 😉)
I made a script that checks netstat for ESTABLISHED connections to 25565. If there aren't any, put all java servers into STOP mode. This way the processes are just put into sleep and they will not take any CPU anymore. They can also quickly be CONTinued if necessary.
Below the contents of the script. You can put it somewhere in your home folder and call it minecraft_sleep.sh
#!/bin/sh
# Check if there are still connections made to minecraft server, otherwise PAUSE the process so that it won't take any CPU anymore.
# Also check /lib/systemd/system/knockd.service
if ! netstat -tn | grep 25565 | grep ESTABLISHED ; then killall -u minecraft -STOP java ; fi
Don't forget to make it executable:
sudo chmod ugo+x minecraft_sleep.sh
Then make a cronjob that runs that script every few minutes. Edit cronjob:
sudo crontab -e
Put this in the cronjob:
*/10 * * * * /home/michiel/minecraft_sleep.sh
Ok, sleeping the process is finished, now let's wake it up when a user connects.
I installed knockd which is a port knocking application that let you run a command when specific ports are knocked.
sudo apt-get install knockd
I figured that knocking on the default 25565 should be sufficient to start the process again so I changed the configuration file at /etc/knockd.conf:
[options]
# UseSyslog
logfile = /var/log/knockd.log
[openMineCraft]
sequence = 25565
seq_timeout = 1
start_command = /usr/bin/killall -CONT java
tcpflags = ack
Edit: Isaac (see a few posts below) found out that he had to include a user for the start command (-u USERNAME). Since my start command ran as root it was probably not needed.
The above configuration has only one sequence, port 25565. Then, when a client connects on that port of the server, knockd invokes '/usr/bin/killall -CONT java' which CONTinues all java process which were STOPped before by the cronjob.
One problem of the knockd service is that it was not able to execute the command "sudo /usr/bin/killall -CONT java" which took me hours and hours to find out.
In the end I found out that there is a service file for knockd located at '/lib/systemd/system/knockd.service'
You have to add some extra permissions for knockd to be able to CONTinue the java processes. Make sure CapabilityBoundingSet= looks like below, and don't put any linebrakes in between (credits to Isaac too). Copy paste of this code below doesn't seem to work because of those linebrakes.
CapabilityBoundingSet=CAP_NET_RAW CAP_NET_ADMIN CAP_CHOWN CAP_DAC_OVERRIDE CAP_DAC_READ_SEARCH CAP_FOWNER CAP_FSETID CAP_KILL CAP_SETGID CAP_SETUID CAP_SETPCAP CAP_LINUX_IMMUTABLE CAP_NET_BIND_SERVICE CAP_NET_BROADCAST CAP_NET_ADMIN
CAP_NET_RAW CAP_IPC_LOCK CAP_IPC_OWNER CAP_SYS_MODULE CAP_SYS_RAWIO CAP_SYS_CHROOT CAP_SYS_PTRACE CAP_SYS_PACCT CAP_SYS_ADMIN CAP_SYS_BOOT CAP_SYS_NICE CAP_SYS_RESOURCE CAP_SYS_TIME CAP_SYS_TTY_CONFIG CAP_MKNOD CAP_LEASE CAP_AUDIT
_WRITE CAP_AUDIT_CONTROL CAP_SETFCAP CAP_MAC_OVERRIDE CAP_MAC_ADMIN CAP_SYSLOG CAP_WAKE_ALARM CAP_BLOCK_SUSPEND CAP_AUDIT_READ
These permissions are too many but I'm too stupid to find out which are necessary and which are not. I'm already happy that it's working 😃
After changing the service file, reload systemctl and the knockd service:
sudo systemctl daemon-reload
sudo systemctl restart knockd
It took me many hours and it's working finally. I thought let's share it with the rest of the world.
Cheers,
Michiel
Can you please tell me what the fix was?
I have read this thread again but I can't find it.