Minecraft Blogs / Tutorial

Automatically start and stop (!) a server when nobody's playing

  • 21,835 views, 1 today
  • 4
  • 0
  • 7
kjoe's Avatar kjoe
Level 40 : Master Architect
12
When you run a server that only has players for a few hours a day, you can save memory on your server by switching the server off when nobody's on.

This tutorial shows how to stop it when it's empty but just as importantly, how to start it when people want to get in. This can all be done without any plugins or mods. it only needs two scripts and a linux box.

In essence, what it does it to listen to the default minecraft server port 25565 and when a connection comes in, it starts the server and then passes all network traffic from the default port to the minecraft server port which needs to be different from default.

So in the server.properties, set "server-port=25555".

Next, the startup script to start the server uses 'screen' to start the server in the background. Create a 'start.sh" in your minecraft directory with the following content:
  • #!/bin/bash
  • cd "$(dirname "$0")"


  • # Change the next variables if needed
  • RUN="bukkit.jar"
  • USERNAME="minecraft"
  • MINMEM="256M"
  • MAXMEM="1280M"


  • if ! pgrep -U "$USERNAME" -f "$RUN" > /dev/null ; then
  • screen -dmS minecraft sudo -u "$USERNAME" -- /usr/bin/java -XX:ParallelGCThreads=2 -Djava.net.preferIPv4Stack=true -Xmx$MAXMEM -Xms$MINMEM -jar "$RUN"
  • echo -en "xFFx00x16x00"
  • echo -n "$MESSAGE" | sed 's/./x0/g'
  • echo -en "xA7x00x30x00xA7x00x35"
  • fi


  • IP=$(sed -n 's/^server-ips*=s*([0-9.]*)s*$/1/p' server.properties)
  • PORT=$(sed -n 's/^server-ports*=s*([0-9]*)s*$/1/p' server.properties)


  • exec sudo -u "$USERNAME" nc "${IP:-127.0.0.1}" "${PORT:-25555}"
Next script is a script to stop the server. It uses "screen" to communicate with the server and look for players. If there's nobody online, it waits a bit and then stops it. So, create a 'stop.sh' with this content:

  • #!/bin/bash

  • cd $(dirname $0)
  • scan=minecraft
  • pl() {
  • screen -S $scan -p 0 -X stuff 'who15'
  • echo $(tail -n 1 /opt/minecraft/server.log | cut -f 6- -d ' ' | wc -m)
  • }


  • [ $(pl) -lt 5 ] && {
  • echo NO players connected
  • sleep 300
  • screen -S $scan -p 0 -X stuff 'who15'
  • [ $(pl) -lt 5 ] && {
  • screen -S $scan -p 0 -X stuff 'stop15'
  • }
  • }
Next we use xinetd to listen to the port and start the 'start.sh' script when somebody connects. So create a file /etc/xinetd.d/minecraft and put this in:

  • # 25565 stream tcp nowait root /srv/minecraft/connectserver
  • service minecraft
  • {
  • disable = no
  • type = UNLISTED
  • port = 25565
  • socket_type = stream
  • protocol = tcp
  • user = root
  • wait = no
  • server = /opt/minecraft/start.sh
  • max_load = 20.0
  • }
  • #change the 'server' setting to whatever dir minecraft is in.
Now to stop the server, we put a line in the crontab. So as root user, type 'crontab -e' and add:

  • */15 * * * * /opt/minecraft/stop.sh
  • #again, edit it to match de minecraft server directory

And that's it. Reload your xinetd and wait 15 minutes for the server to shutdown.
Tags

Create an account or sign in to comment.

1
09/11/2019 7:53 amhistory
Level 1 : New Explorer
gekigek99
gekigek99's Avatar
check out this link (it does the same thing but in a more professional way with python):


[url=/blog/automatically-stop-a-server-when-nobodys-playing/<a href=]github.com/gekigek99/minecraft-vanilla-server-hibernation[/url]


and it's updated to 2019 if someone needs it!!!
1
05/30/2017 3:43 pm
Level 1 : New Miner
lloyd249
lloyd249's Avatar
this does not help me.
1
10/06/2013 7:44 pm
Level 1 : New Miner
CQFD
CQFD's Avatar
Is there a possibility to adapt this to Mac OS X ?
Because, it looks so COOL.
1
10/12/2013 7:02 pm
Level 1 : New Miner
CQFD
CQFD's Avatar
I'm actually trying to adapt this to Mac OS X myself (I'll describe the complete setup if successful...).

I have some questions concerning what is decribed above :
  • In the start.sh file, it seems to me that the start script is redirecting all the traffic to port 25555 with the nc command.
  • Also, once the redirection step has occured, the redirection to port 25555 does not end when the server stops.
My understanding is that this tutorial presumes the minecraft server runs on a dedicated machine.

I would like to use my own Mac to do so under a dedicated user account (setup according to the tutorial found here : Create a Mac OS X startup daemon).

For this purpose, is it OK to modify the redirection like this :
exec sudo -u "$USERNAME" nc -p 25565 "${IP:-127.0.0.1}" "${PORT:-25555}"

Also, if necessary, how do I stop/clear the redirection to port 25555 ?

Finally, what are those lines for ? :
  • echo -en "xFFx00x16x00"

  • echo -n "$MESSAGE" | sed 's/./x0/g'
  • echo -en "xA7x00x30x00xA7x00x35"
Please, can somebody help me with that.
1
10/07/2012 11:49 am
Level 35 : Artisan Mage
tomenluc
tomenluc's Avatar
So this is for a bukkit server with a windows computer?? - Because I really love the idea!!!
1
10/20/2012 7:08 am
Level 40 : Master Architect
kjoe
kjoe's Avatar
this is actually for a linux computer. the tools these scripts use are not available on windows unless you use something like cygwin. then you could use this on a windows machine too.
1
09/28/2012 5:35 pm
Level 1 : New Explorer
superdisc_
superdisc_'s Avatar
Sweet! This is really helpful!
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome