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.

He this my seem a little basic and simple but I never did this before.

I have this minecraft server and It starts up wit a batch file containing:

java -Xms1012M -Xmx1012M -jar craftbukkit-1.5.1-R0.1.jar -o true
PAUSE

Now I really simple want to create a batch that stops the server. In cmd I can just type in stop and the server stops. How can I pass this command to the running java application.

Some pseudo code:

java craftbukkit-1.5.1-R0.1.jar stop

I really have no idea. I searched the internet But I only find tutorials about making a start.bat.

A link with a tutorial will also make me happy. It just happens to be I can't find a good tutorial which covers this.

Now I found this script for linux:

 #!/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

But my server is running an a WindowsXP machine so is there a way to do the above in windows? I have no idea.

share|improve this question

migrated from stackoverflow.com Apr 9 '13 at 1:42

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

add comment

3 Answers

up vote 1 down vote accepted

I found a way to do this with AutoHotkey:

Step 1: Download->autohotkey.exe

Step 2: Install->autohotkey.exe

Step 3: Write this script and call it (stop.ahk):

SetTitleMatchMode, 2      ; mode 2 = title only needs to contain string to match
SetKeyDelay, 0            ; set key delay to minimal
#WinActivateForce         ; automatically use more forceful methods
IfWinExist, run-minecraft ; check if there is a cmd window with this title
{                         ; send the commands
    ControlSend, ,say Server will shutdown in 10 seconds, run-minecraft
    ControlSend, ,{enter}
    sleep, 9999
    ControlSend, ,stop, run-minecraft
    ControlSend, ,{Enter}, run-minecraft
    return
}

Step 4: Put title run-minecraft In front of your start.bat

Step 5: Left click (stop.ahk) And select compile script

Step 6: run application with windows task-manager

share|improve this answer
add comment

If you just want to kill the process, you can do something like this in your bash script:

ps -ef | grep java | awk '{print$2}' | xargs -i kill {}

You may want to replace 'java' with a more specific identifier.

If you want to stop the process more gracefully, you'll have to implement something like a socket listener. In that case, you could create a simple client app to send a message to the running app, over a port, telling it to shut itself down. For more info, see this tutorial.

share|improve this answer
add comment

Taskkill should do the trick, see http://technet.microsoft.com/en-us/library/bb491009.aspx. But I think the tricky part would be to figure out which java process to kill and I think jps might help. More information here http://yannickloriot.com/2012/08/kill-a-java-process-from-the-command-line-or-a-bat-file/#sthash.t36Ff6C8.dpbs

share|improve this answer
    
I am afraid the server will be damaged if I just kill the process –  sirwilliam Apr 8 '13 at 19:39
    
If you start the java process with an image name, it shouldn't be an issue. Why do you think the server will be damaged? –  Mahesh Guruswamy Apr 8 '13 at 20:23
    
Because now I shutdown using taskkill /F /IM java.exe When doing this the server will create an unsuspected stop. Most of the time it will just result in some unsaved changes. I want to stop the server the right why so it saves everything etc. This is possible by using stop command in cmd gui where the java.exe is running the .jar. So is there a way to forward that command to the java.exe through a batch file? –  sirwilliam Apr 8 '13 at 20:30
    
The only I see is to start digging through the minecraft code and see what gets invoked when you type in the stop command, and see if you can replicate that outside of the server. –  Mahesh Guruswamy Apr 8 '13 at 20:42
    
I shal do that. Just figuring out how to do this with eclipse and creating a class to stop the server issuing the stop method of the server class. –  sirwilliam Apr 8 '13 at 21:08
add comment

Your Answer

 
discard

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

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