So, I want to create a batch file for my Minecraft Server to startup and then restart the server at the end of each day (midnight, preferably, but 24-hour period works as well)

This is my startup batch, which calls other batch files to open and start the servers individually:

start cmd /k call BungeeStartup.bat
start cmd /k call HUBStartup.bat
start cmd /k call PvEStartup.bat
start cmd /k call PvPStartup.bat
start cmd /k call MinigamesStartup.bat
start cmd /k call TownyStartup.bat
start cmd /k call SecQStartup.bat

And each "Startup.bat" calls to run the server like this:

cd Minecraft 1.12 Server Files
cd BungeeCord   <-- and each it's respective server, of course, so I don't have to type it all out again

And, finally, the RUN.bat files look like this:

java -jar -Xmx350M spigot-1.12.2.jar -o true

And that starts all of my servers up in different command prompts. But the issue is, there's no way (that I know of) with this to end the servers without me manually typing /stop into each command prompt server, and then 'exit' into each command prompt again to exit it. Then I have re-execute the batch file manually as well.

So, my questions are:

  • How do I (with the batch files) end each server, and then each command prompt window, without doing it manually
  • How do I re-start them automatically after they exited.
  • AND how to get this specifically to happen once every 24 hours (or, preferably at midnight each night)


So, to clarify how the Minecraft server works, I'm adding this edit.

When you startup a server, obviously it runs the server and everything and people can connect and play. It has a command prompt that you use to execute commands and see information and all that. In order to properly shut-down the server to where it saves everything and doesn't corrupt files, you type 'stop' into the command prompt. Then it exits out of the server and normally would exit the command prompt for you to re-open as willing. Then, with the batch files I have set up, it doesn't just exit the command prompt after typing 'stop', it returns you to the directory for you to continue using the command prompt as if it's not running the server anymore (because it isn't), and you have to type 'exit' to close the command prompt.


MineCraft Game App Stop and Restart Automation - Windows

I've included a two part batch script solution. The first part will require setting a title that matches the app name string so the running app commands contain this meta data. The second part will use a combination of appactivate to active the command windows with the app name titles, and sendkeys to emulate the keyboard strokes to close and restart the apps.

1. Batch Script (set title)

Note: You simply add a new line of title "<appname>" in the same batch script you currently use but on the line above the java -jar ~ commands you run. You will obviously need to replace the <appname> value to be the name of the app you need to stop, etc.

title "BungeeCord"
java -jar -Xmx350M spigot-1.12.2.jar -o true

2. Batch Script (stop app and then restart it)

Notes: You will want to set the AppName= value to be the same value and case as you use for each script where you set the title. The StopApp= value should be set to the value which you use that is typed in to stop the correlated app e.g. end, stop, etc.


SET AppName=BungeeCord
SET StopApp=end
SET StopPauseSec=5
SET StartPauseSec=10

SET TempVBSFile=%temp%\~tmpSendKeysTemp.vbs
IF EXIST "%TempVBSFile%" DEL /F /Q "%TempVBSFile%"

ECHO Set WshShell = WScript.CreateObject("WScript.Shell") >>"%TempVBSFile%"
ECHO Wscript.Sleep 900                                    >>"%TempVBSFile%"
ECHO WshShell.AppActivate "%AppName%"                     >>"%TempVBSFile%"
ECHO Wscript.Sleep 900                                    >>"%TempVBSFile%"
ECHO WshShell.SendKeys "%StopApp%"                        >>"%TempVBSFile%"
ECHO Wscript.Sleep 500                                    >>"%TempVBSFile%"
ECHO WshShell.SendKeys "{ENTER}"                          >>"%TempVBSFile%"
ECHO Wscript.Sleep %StopPauseSec%000                      >>"%TempVBSFile%"
ECHO WshShell.SendKeys "%%{F4}"                           >>"%TempVBSFile%"
CSCRIPT //nologo "%TempVBSFile%"

timeout /t %StartPauseSec%
start "" "cmd /k call BungeeStartup.bat"


You can use Task Scheduler to create a new task. From the General tab, give job an applicable name, and then be sure to check both the Run only when user is logged on and Run with highest privileges options.

enter image description here

From the Triggers tab select New options to create a new trigger. Then be sure Begin this task field is set to On a schedule and the Settings options are set as One time with the Start field values being the the date it'll be at midnight where you are now (e.g. 7/12/2018) and the time value being set as 12:00:00 AM to ensure it'll run at that specific time. Now down in the Advanced settings section, you want to ensure the Repeat task every is at a 24 hour value (type it in manually if needed) and the for a duration of is set to an indefinitely value and this will ensure it's kicked off every night at 12 midnight.

enter image description here

From the Actions tab, select the New option to create a new action. Then in the New Action window be sure the Action field is set to Start a program and the Program/script field has the full path to the script you need to run, and the Start in field has the folder path only which the script reside that will run. Press OK and OK again and everything should be automated now.

enter image description here

Further Resources

  • @FireDragon Okay, for the timeout /t %StartPauseSec% line you can make it be ::timeout /t %StartPauseSec% to comment it out and then run without it to see if it works without that or not. If it works, then you can remove or comment out the ::SET StartPauseSec=10 line as well. I couldn't fully test these processes on my side but having a small wait period for certain things never hurts and usually always helps me for the things I automate with these type of solutions but I don't have Mine Craft server on my side to fully test. – Pimp Juice IT Jul 11 '18 at 22:03
  • What I could do is schedule each event after the other is supposedly finished, but I'd rather not have to do it that way – Fire Dragon Jul 11 '18 at 22:45
  • Alright, well with my testing this, it closes BungeeCord, closes the HUB, and the re-opens the HUB, closes it, opens a new PvE without closing the already-existing one, closes it, and does the same for all the other servers, ending with SecQ but not closing out of it. – Fire Dragon Jul 13 '18 at 14:07
  • So I took a video of what happens exactly, and it also shows the setup and batch file codes I have and stuff. How can I share it with you? – Fire Dragon Jul 13 '18 at 19:53
  • Also, it's one screen just so you know – Fire Dragon Jul 13 '18 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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