1

I have a minecraft server running in screen, and right now, it is just a script that starts java like so:

screen -dmS mc java -Xincgc -Xms1024M -jar craftbukkit-1.6.4-b2882.jar

Basically this just starts minecraft in a screen container in the background, and I can access it with screen -r mc through SSH. However, what I would like to do, is have the process always up on the server monitor, and also accessible through ssh with screen -x mc. Also, I have a restart.sh script that stuffs the stop command to the mc process every two hours, then it executes the start.sh script. The server is at runlevel 3, so I don't think gnome-terminal -e "screen -x mc" will work. Also, it seems like the restart script is starting mc outside of screen. When I SSH into the server, screen -ls turns up nothing, but the minecraft server process is running and I can connect to it. Here's the script:

#!/bin/sh

screen -x mc
screen -s mc stuff "say Server Restarting in 15 minutes."
screen -s mc -X eval "stuff \015"
sleep 600
screen -x mc
screen -x mc -X stuff "say Server restarting in 5 minutes."
screen -s mc -X eval "stuff \015"
sleep 240
screen -x mc
screen -s mc -X stuff "say Server restarting in 1 minute."
screen -s mc -X eval "stuff \015"
sleep 60
screen -S mc -X stuff "say Server restarting."
screen -S mc -X eval "stuff \015"
screen -S -X stuff "kickall Server Restarting, it should be back up in about a minute."
screen -S mc -X eval "stuff \015"
sleep 2
screen -S mc -X stuff "stop"
screen -S mc -X eval "stuff \015"
sleep 30
screen -wipe
sleep 3
cd /minecraft/server/craftbukkit
./start.sh

Any ideas?

1

There are four parts to this.

  1. GNU screen
  2. a script to start minecraft and restart it if it dies (e.g. call that start-minecraft.sh)
  3. a boot-time script to run that start-minecraft.sh script inside screen. It should use su or sudo to run start-minecraft.sh under the uid that will own the screen session and the minecraft server process. This can be run from, e.g., /etc/inittab or /etc/rc.local.
  4. a cron job to kill the minecraft server every two hours.

start-minecraft.sh should be a simple endless-loop wrapper around minecraft with perhaps a short sleep between mc restarts. A minimalist version of the script would be something like this:

#! /bin/sh

# set PATH and other env variables here, if required
# ...

while true; do
   java -Xincgc -Xms1024M -jar craftbukkit-1.6.4-b2882.jar
   sleep 30
done

To run that script inside screen, one way to do it is like this:

screen -d -RR minecraft -t minecraft /path/to/start-minecraft.sh

That tells screen to attach to an existing screen session called "minecraft" if it exists, or create a new one with that name if it doesn't, and then create a single tab inside screen with title "minecraft" and run the start-minecraft.sh script inside that.

If the user running screen has a ~/.screenrc file, you may also want to override that with either no config file or a custom one using screen's -c option - e.g. screen -c /dev/null ...

Which brings us to the second way of starting screen. Create a custom .screenrc file for minecraft which includes the following two lines:

screen -t bash  1 bash
screen -t minecraft  0 /path/to/start-minecraft.sh 

This will create two "windows" inside screen, one running bash, the other running minecraft. It starts minecraft last and on window 0 so it will be the first one shown when you connect to it.

Whicever method you use to start screen, you can manually attach to this screen session later (e.g. from your terminal or over a ssh connection) with screen -d -RR minecraft.

The latter (custom ~/.screenrc) method is preferred. If the start-minecraft.sh ever dies (e.g. because you killed it to take the minecraft server offline temporarily), you can restart it just by creating a new "window" in screen and running start-minecraft.sh manually.

  • If I did create a bash screen session and a minecraft session, could I attach the minecraft session inside the bash session at the end of the start.sh script? I tried it, and I got eh bash session to automatically attach to the main server terminal, but when I tried to do screen -s bash -X stuff "screen -r mc" then screen -s bash -X stuff "/015" at the end of the script nothing happened. – InventiousTech Sep 24 '13 at 13:28
  • If you have an existing screen with session name 'main', then you can create a new window in that session with the title 'minecraft' with screen -S main -X screen -t minecraft. You can then tell it to run a command - e.g. start-minecraft.sh with screen -S main -p minecraft -X stuff '/path/to/start-minecraft.sh^M'. Note that the ^M is a Ctrl-M character (i.e. in bash, type Ctrl-V Ctrl-M). – cas Sep 25 '13 at 0:27
  • Note that you could just run start-minecraft.sh in a new screen window with screen -S main -X screen -t minecraft /path/to/start-minecraft.sh but when that command terminates, the screen window will close. opening a shell and then pasting a command into it allows the screen window to stay around until the shell exits. – cas Sep 25 '13 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.