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


Get Embed Codes

Level 28 : Expert Architect
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:-}" "${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:Low Use, Start, Stop, Crontab, Script, Tutorial

Comments : 5

Join us to post comments.

Show Comments

1 - 5 of 5

  • CQFD
  • Level 1
  • New Miner
  • October 6, 2013, 3:44 pm
Is there a possibility to adapt this to Mac OS X ?
Because, it looks so COOL.
  • CQFD
  • Level 1
  • New Miner
  • October 12, 2013, 3:02 pm
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 :
[size=13px]exec sudo -u "$USERNAME" nc -p 25565 "${IP:-}" "${PORT:-25555}"[/size]

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

Finally, what are those lines for ? :
  • [size=13px]echo -en "xFFx00x16x00"[/size]

  • [size=13px]echo -n "$MESSAGE" | sed 's/./x0/g'[/size]
  • [size=13px]echo -en "xA7x00x30x00xA7x00x35"[/size]
Please, can somebody help me with that.
  • tomenluc
  • Level 35
  • Artisan Mage
  • October 7, 2012, 7:49 am
So this is for a bukkit server with a windows computer?? - Because I really love the idea!!!
  • kjoe
  • Level 28
  • Expert Architect
  • October 20, 2012, 3:08 am
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.
  • superdisc_
  • Level 1
  • New Explorer
  • September 28, 2012, 1:35 pm
Sweet! This is really helpful!

1 - 5 of 5

Show Comments