1

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

1 diamonds

  • 418
    Views, 1 today
  • 3
    Comments
  • 0
    Favorites
  • Flag / Report

Get Embed Code

Forum:
HTML:
Link:
avatar kjoe
Level 15 : Journeyman Architect
Posted 09/28/12 5:31:42 pm
09/28/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. 

Additional Details

Tags:Low Use, Start, Stop, Crontab, Script, Tutorial

Join us to post comments.

Comments : 3

1 - 3 of 3

tomenluc
Level 29
Expert 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 15
Journeyman 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 - 3 of 3