Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm new here and I couldn't find this anywhere else. I wanted to write a quick bash script so I could stop having to copy/paste the following code into my linux server.

screen -S MineCraft -d -m java -Xincgc -Xmx1G -jar craftbukkit-0.0.1-SNAPSHOT.jar

I want to be able to punch in a simple command that will run the above command, IE: Minecraft or something of that sort.

share|improve this question

migrated from stackoverflow.com Oct 17 '12 at 9:27

This question came from our site for professional and enthusiast programmers.

1  
By the way, this isn't a Linux support website, this is for questions specific to programming. I answered your question for you, but you'd be better off at SuperUser (google it). –  SpikeX Oct 18 '11 at 13:49

4 Answers 4

put

alias minecraft="screen -S MineCraft -d -m java -Xincgc -Xmx1G -jar craftbukkit-0.0.1-SNAPSHOT.jar"

in your .bashrc file

share|improve this answer

This is the shell script I've written for my Minecraft server.

save it as start.sh and call it with ./start.sh start

You can also call backup, which creates a backup while the server can still run. This backup can be restored later.

Remember to change craftbukkit.jar into whatever your JAR file is.

    #!/bin/bash

    # Made by Devils Child.
    ##############################################################
    DIRECTORY='/home/minecraft'
    SESSIONNAME='minecraftserverscreen'
    JARFILE='craftbukkit.jar'
    WORLDNAME='world'
    NETHERNAME='world_nether'
    SKYNAME='world_skylands'
    ENDNAME='world_the_end'
    ##############################################################

start()
{
    SCREENID=$(screen -ls | grep -w $SESSIONNAME | cut -f 2)
    if [ $SCREENID ]; then
        echo "Minecraft is already running."
    else
        echo "Starting Minecraft server..."
        screen -dmS $SESSIONNAME java -Xms768M -Xmx768M -jar $JARFILE nogui
    fi
}

stop()
{
    SCREENID=$(screen -ls | grep -w $SESSIONNAME | cut -f 2)
    if [ $SCREENID ]; then
        echo "Stopping Minecraft server..."
        screen -S $SESSIONNAME -p 0 -X stuff "`printf "stop\r"`"
        while screen -ls | grep $SESSIONNAME > /dev/null
        do
            sleep 0.1
        done
    else
        echo "Minecraft is not running."
    fi
}

restart()
{
    echo "Restarting Minecraft server..."
    stop > /dev/null && start > /dev/null
}

saveoff()
{
    screen -S $SESSIONNAME -p 0 -X eval 'stuff "say Starting backup..."\015'
    screen -S $SESSIONNAME -p 0 -X eval 'stuff "save-off"\015'
    screen -S $SESSIONNAME -p 0 -X eval 'stuff "save-all"\015'
    sync
}

saveon()
{
    screen -S $SESSIONNAME -p 0 -X eval 'stuff "save-on"\015'
    screen -S $SESSIONNAME -p 0 -X eval 'stuff "say Backup complete."\015'
}

backup()
{
    echo "Creating backup of '$WORLDNAME', '$NETHERNAME', '$SKYNAME' and '$ENDNAME'..."
    saveoff
    sleep 5
    mkdir -p backups
    tar cfv "backups/NEW.tar" $WORLDNAME $NETHERNAME $SKYNAME $ENDNAME > /dev/null
    mv "backups/NEW.tar" "backups/$(date +"%m-%d-%Y_%H.%M.%S").tar"
    saveon
}

restore()
{
    echo "Restoring '$WORLDNAME', '$NETHERNAME', '$SKYNAME' and '$ENDNAME'..."
    stop > /dev/null
    rm -r $WORLDNAME
    rm -r $NETHERNAME
    rm -r $SKYNAME
    rm -r $ENDNAME
    tar xfv "backups/$(ls backups -t | head -1 | grep ".tar")" > /dev/null
    start > /dev/null
}

cd $DIRECTORY

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    backup)
        backup
    ;; 
    restore)
        restore
    ;;
    *)
        echo "Usage: ${0} {start|stop|restart|backup|restore}"
        exit 2
esac
exit 0
share|improve this answer
    
How can I do this with Windows? –  sirwilliam Apr 9 '13 at 7:40
    
To be honest, I don't know as I'm only hosting a Linux server. –  Devils Child Apr 10 '13 at 11:33

First:

$ nano runMinecraft

Type this in:

#!/bin/bash
screen -S MineCraft -d -m java -Xincgc -Xmx1G -jar craftbukkit-0.0.1-SNAPSHOT.jar`

Save it, type this in:

$ chmod +X runMinecraft

Then, to run Minecraft, simply type:

$ ./runMinecraft

You can also copy runMinecraft to /usr/bin/ so you don't have to type the ./ every time you run it.

share|improve this answer

Assuming you have ~/.profile and ~/.bash_aliases files, add an alias to ~/.bash_aliases (man alias for more information)

Alternatively (Also reliant on ~/.profile) add an executable bash script to ~/bin with the correct shebang (#!/bin/bash) and it should also run.

If missing the files, copy them from /etc/skel

cd /etc/skel
cp .profile .bash_aliases ~
echo 'alias minecraft="screen -S MineCraft -d -m java -Xincgc -Xmx1G -jar craftbukkit-0.0.1-SNAPSHOT.jar"' | tee -a ~/.bash_aliases

relog and it should work

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.