编写Windows批处理脚本实现多Spigot Minecraft服务器优雅关停
Absolutely! You can automate the graceful shutdown of your three Minecraft server instances (BungeeCord, Survival, Lobby) with a Windows batch script—no more manually typing stop in each console window. Here's a robust, step-by-step solution tailored to your setup:
Prerequisite: Set Unique Window Titles for Each Server
First, make sure each server's startup batch script sets a distinct window title. This lets our shutdown script target the correct console windows reliably.
For your BungeeCord instance's startup script (start_bungee.bat):
@echo off title BungeeCord-Server java -Xmx512M -jar BungeeCord.jar pause
For your Survival server (start_survival.bat):
@echo off title Survival-Server java -Xmx2G -jar spigot-survival.jar pause
For your Lobby server (start_lobby.bat):
@echo off title Lobby-Server java -Xmx1G -jar spigot-lobby.jar pause
Step 1: Create the SendKeys Helper Script
Windows batch doesn’t natively support sending keyboard input to other windows, so we’ll use a tiny VBScript helper to do this. Create a file named sendkeys.vbs in the same folder as your shutdown script:
Set WshShell = WScript.CreateObject("WScript.Shell") ' Activate the window with the specified title WshShell.AppActivate WScript.Arguments(0) ' Wait 500ms to ensure the window is active before sending input WScript.Sleep 500 ' Send the command followed by an Enter keypress WshShell.SendKeys WScript.Arguments(1)
Step 2: Build the Shutdown Batch Script
Now create your main shutdown script (shutdown_all_servers.bat), which will use the helper script to send stop commands to each server, wait for them to save data, and optionally clean up the console windows:
@echo off echo === Starting Graceful Shutdown of All Minecraft Servers === :: Send stop command to BungeeCord echo Sending stop command to BungeeCord... cscript //nologo sendkeys.vbs "BungeeCord-Server" "stop{ENTER}" :: Send stop command to Survival Server echo Sending stop command to Survival Server... cscript //nologo sendkeys.vbs "Survival-Server" "stop{ENTER}" :: Send stop command to Lobby Server echo Sending stop command to Lobby Server... cscript //nologo sendkeys.vbs "Lobby-Server" "stop{ENTER}" echo All stop commands sent. Waiting for servers to save and shut down... timeout /t 15 /nobreak >nul :: Adjust this timeout (in seconds) based on your server size :: Optional: Force-close console windows if they don't exit automatically echo Cleaning up console windows... taskkill /FI "WINDOWTITLE eq BungeeCord-Server" /F /T >nul 2>&1 taskkill /FI "WINDOWTITLE eq Survival-Server" /F /T >nul 2>&1 taskkill /FI "WINDOWTITLE eq Lobby-Server" /F /T >nul 2>&1 echo === All Servers Shut Down Successfully! === pause
Key Notes & Customizations
- Window Title Accuracy: Double-check that the titles in your shutdown script match exactly what’s set in the startup scripts—typos will break targeting.
- Adjust Wait Time: The
timeout /t 15command waits 15 seconds for servers to save data. If your Survival server has large worlds, bump this to 20-30 seconds to avoid data corruption. - Skip Force Close: If you prefer letting servers close their own consoles automatically, remove the
taskkilllines entirely. - Minimized Windows: Don’t worry if server windows are minimized—
AppActivatewill still target them and send thestopcommand correctly.
内容的提问来源于stack exchange,提问作者MotorBlade

