Use SSH Screens with your Minecraft Server Script

grimdestripador
  10 years ago
  2

There are many ways to administer a minecraft server. This minecraft server tutorial will achieve a boot script which will allow minecraft administrators gain access via ssh in a shared configuration. The ability to work in a shared configuration allows for multiple administrators to be able to log in and out of a "server" computer without having to shut the minecraft server down. This alleaviates the problem of needing to keep a ssh pipe open, for if the ssh connection dies, the minecraft server stops and kicks all users.

This solution is to create a script which ran,

  1. use 'screen' to lauch minecarft in a detached named screen as a daemon (in the background) for if minecraft is being launched the first time
  2. use 'screen' to resume its terminal and give you command line interface with minecraft if the script is being ran while minecraft is already running. Screen has a detach magic key sequence Ctrl-A,D that is pressed to detach, aka logout of minecraft administration, without needing to shut the server down.

Procedure

Install Screen

  • sudo apt-get install screen -y

Create $MINECRAFT_HOME folder and variable so we don't need to type it so much

  • export MINECRAFT_HOME=/opt/minecraft
  • mkdir $MINECRAFT_HOME
  • chmod 775 $MINECRAFT_HOME

Download Minecraft

  • cd $MINECRAFT_HOME
  • wget https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar

Create the file ~/startMinecraft.sh

  • nano ~/startMinecraft.sh

Copy the following and paste the following code into into terminal window with the key combination Ctrl-Insert

  •   #!/bin/bash
    #@Author grimdestripador
    #@Copyright 2013 grimdestripador released as Public Domain
    
    #Configure your minecraft folder which contains minecraft_server.jar and the world folder.
    
    export MINECRAFT_HOME=/usr/local/bin/minecraft
    
    echo Starting backup of $MINECRAFT_HOME
    tar cf ~/mcBackup`date +%s`.tar $MINECRAFT_HOME/*
    echo Backup location: ~/mcBackup`date +%s`.tar
    echo .
    echo .
    echo Spawning background daemon SCREEN named minecraft. 
    echo To resume: type screen -r minecraft. 
    echo To detach from screen: press the magic sequence: ctrl-A, D.
    echo To quit minecraft:1) type at terminal: screen -r minecraft
    echo . . . . . . . . . 2) type: stop
    echo .
    
    #Change user working directory for preparation of "java -jar" command
    cd $MINECRAFT_HOME
    
    #Check to see if a minecraft screen is already running
    lineCount=`screen -r minecraft | grep "There is no screen to be resumed matching minecraft." | wc -l`
    
    #Start the minecraft server in a detached screen named "minecraft" if its not running
    #Launch the command line interface for minecraft if it is arealdy running.
    if [ $lineCount -eq 1 ]
      then
        echo linecount: $lineCount. Starting in a deteched screen named minecraft. Use screen -r minecraft to view.
        screen -dmS minecraft java -Xms1024M -Xmx1024M -jar $MINECRAFT_HOME/minecraft_server.jar nogui
      else
        echo lineCount: $lineCount. Minecraft is already running. Use screen -r minecraft to view. Running now.
        screen -r minecraft
    fi
    
    
  • .
  • .
  • .