batch-filejarminecraftbukkit

Batch script to send commands to gracefully shutdown multiple Spigot Minecraft servers


I'm wondering if it possible to make a windows batch script send commands to three already running .jar file instances. In this case the open .jar files correspond to a bungeeCord instance, a survival instance and a hub instance. Each of these .jar instances together, form the entire Spigot Minecraft server. (See the images)

survival .jar instancehub .jar instancebungeeCord .jar instance

Currently in order to close each of these servers/.jar instances gracefully you need to manually type in the 'stop' command in the command line interface of both the survival and hub instance. And you have to type 'end' to shut down the bungeeCord instance gracefully.

In order to automate this process I would like to write a batch script that allows me to send the 'stop' and 'end' commands to the currently running .jar instances in one click, rather than having to type each command out manually on each .jar instance.

I've actually already managed to automate the start up of each .jar instance process by writing the following batch script which may help:

echo off

cd D:\Projects\Servers\"CRAFT412 Server Desktop"\BungeeCord
start "BungeeCord" java -Xms512M -Xmx512M -jar BungeeCord.jar

cd D:\Projects\Servers\"CRAFT412 Server Desktop"\hub
start "hub" java -Xms512M -Xmx1G -XX:+UseConcMarkSweepGC -jar spigot.jar

cd D:\Projects\Servers\"CRAFT412 Server Desktop"\survival
start "survival" java -Xms512M -Xmx1G -XX:+UseConcMarkSweepGC -jar spigot.jar

Upon running this script all three required .jar instances are opened and ran like the image above.

It may also help if you can see the folder structure as well so I've provided an image of that:

enter image description here

The start.bat file corresponds to the code above to startup all three .jar instances.

Any help would be appreciated.

Thanks.


Solution

  • Do not use taskkill, killing the process results data loss!
    I found a nice tool called SendMessage it can send keys to processes. I wrote a little batch script, but it has some errors and I can't find the sources of these errors, but if you repair the code you have a working batch script for your problem. (I know that I shouldn't post not working code as answer, but it contains the logic and you can form this code as you want.)

    @echo off
    cls
    
    ::Declare servers by title
    ::For BungeeCord servers add "BCS" prefix
    set servers[0]=BCStest1 
    set servers[1]=test2
    set servers[2]=test server
    
    ::Servers in the array minus 1
    set len=2
    ::Loop trought the servers
    for /l %%s in (0,1,%len%) do (
        ::Set the server variable to the current server, depending on the loop number
        set server=%servers[%%s]%
        
        ::Testing for the server type
        if "BCS"=="%server:~0,3%" (
        ::If it's a BungeeCord server
        
            ::Removing the prefix
            set server=%server:~3%
            
            ::Sending shutdown command
            echo Sending shutdown command to server: %server%
            
            ::e
            SendMessage-1.1.2.exe /windowtitle:%server% /message:WM_CHAR /wparam:101 /lparam:1
            ::n
            SendMessage-1.1.2.exe /windowtitle:%server% /message:WM_CHAR /wparam:110 /lparam:1
            ::d
            SendMessage-1.1.2.exe /windowtitle:%server% /message:WM_CHAR /wparam:100 /lparam:1
            :: ENTER
            SendMessage-1.1.2.exe /windowtitle:%server% /message:WM_CHAR /wparam:13 /lparam:1
        
        ) else (
        ::If it's a normal server
        
            ::Sending shutdown command
            echo Sending shutdown command to server: %server%
            
            ::s
            SendMessage-1.1.2.exe /windowtitle:%server% /message:WM_CHAR /wparam:115 /lparam:1
            ::t
            SendMessage-1.1.2.exe /windowtitle:%server% /message:WM_CHAR /wparam:116 /lparam:1
            ::o
            SendMessage-1.1.2.exe /windowtitle:%server% /message:WM_CHAR /wparam:111 /lparam:1
            ::p
            SendMessage-1.1.2.exe /windowtitle:%server% /message:WM_CHAR /wparam:112 /lparam:1
            :: ENTER
            SendMessage-1.1.2.exe /windowtitle:%server% /message:WM_CHAR /wparam:13 /lparam:1
        )
    )
    
    ::If the execution gets here it was successfull
    echo Stop command was sent to all server!
    pause
    

    (Sorry, I used batch realy long time ago, this is the reson for that I can't provide fully working code.) And don't forget to download the SendMessage-1.1.2.exe and put it into the same directory as the *.bat file!