Sunday, December 16, 2018

windows - Batch File Automation issues


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:


D:
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
RUNGEE.bat

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)


EDIT:


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.


Answer



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 "" 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 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.


@ECHO OFF
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"
EXIT /B



Scheduling


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


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...