How to execute a command after my batch file loads a .jar - batch-file

I've got a batch file that starts a .jarfile but after it finishes loading the .jar, it doesn't execute the other commands that run after.
Here's my code:
#echo off
title DWAD
color 0A
cls
:start
echo loading server...
java -Xms3G -Xmx20G -jar DWAD.jar nogui
echo "/say server will restart in 60 seconds"
TIMEOUT /T 30
echo /say server will restart in 30 seconds
TIMEOUT /T 10
echo /say server will restart in 10
TIMEOUT /T 1
echo /say 9
TIMEOUT /T 1
echo /say 8
TIMEOUT /T 1
echo /say 7
TIMEOUT /T 1
echo /say 6
TIMEOUT /T 1
echo /say 5
TIMEOUT /T 1
echo /say 4
TIMEOUT /T 1
echo /say 3
TIMEOUT /T 1
echo /say 2
TIMEOUT /T 1
echo /say 1
TIMEOUT /T 1
echo /say server restarting
How do I instruct the code to run the next command "echo" after the .jar has finished loading?
Thank you!
I've tried using a call command before java -Xms3G -Xmx20G -jar DWAD.jar nogui without luck
I've tried using a /wait command after java -Xms3G -Xmx20G -jar DWAD.jar nogui without luck too
What I expect is for the batch file to run the next line of code after loading the .jar but the result is that it doesn't. The batch file just stops right after it loads the .jar

OP would like to script a Minecraft Server
I have found the plugin I believe I used to do this in the past, it is located here:
https://www.minecraftforum.net/forums/mapping-and-modding-java-edition/minecraft-tools/1263927-win-mac-linux-minecraft-console-client
This should allow him to pass text and commands to the user sin the server while performing functions on the OS too, allowing him to automate the server's shut down as he's looking to do.
Double checked and again looks like the one I used 8 years ago when I had a server for a bit, and as the plugin author says:
• Single command usage : MinecraftClient.exe username password server
"/mycommand"
This will automatically send "/mycommand" to the server, and then the
client will automatically leave the server and close.
Useful for use in batch command sending scripts.
This would change your script to be something like this:
#(
SETLOCAL
ECHO OFF
SET "_UN=ServerUserName"
SET "_PW=ServerPassword"
SET "_IP=ServerIP"
title MinecraftClient
color 0A
cls
)
CALL :Main
(
ENDLOCAL
Exit /B 0
)
:Main
ECHO.
ECHO. Begining Script:
REM Letting the Users Know about the Count Down
FOR /L %%L IN (60,-1,1) DO (
MinecraftClient.exe %_UN% %_PW% %_IP% "/say WARNING: Server will Restart in %%L Seconds!"
Timeout 1
)
ECHO.
ECHO. Sending Final Notice and Shutting down the Server!
MinecraftClient.exe %_UN% %_PW% %_IP% "/say !!! WARNING: Server GOING DOWN FOR RESTART NOW !!!"
REM Not sure what the Restart command is offhand.
MinecraftClient.exe %_UN% %_PW% %_IP% "/Restart Command"
GOTO :EOF

Related

Trying to auto restart minecraft server every 3 hours

I used the code provided in the best answer in this thread: Need auto-restart script in batch for minecraft server
However, I'm not sure when the choice function is supposed to run.
Furthermore, I'd rather not have a choice option. I'd like the server just to give a 60 second heads up that it's going to restart and then execute the restart.
Any help would be appreciated!
Here's the code from the previous answer:
#echo off
title minecraft-server-1.8.3
color 0A
prompt [server]:
cls
:start
echo loading server...
java -Xms3G -Xmx3G -jar minecraft_server.1.8.3.jar nogui
cls
:choice
set /P a=do you want to restart[Y/N]?
if /I "%a%" EQU "Y" goto :restart
if /I "%a%" EQU "N" goto :stop
goto :choice
:restart
cls
echo server will restart
TIMEOUT /T 5
cls
goto :start
:stop
cls
echo closing server
TIMEOUT /T 5
exit
Welcome to stack overflow.
As #Mofi mentioned in a comment, you can use TIMEOUT to create wait statement
The script you might want would look something like this:
:start
echo loading server...
java -Xms3G -Xmx3G -jar minecraft_server.1.8.3.jar nogui
cls
REM I recommend NOT using TIMEOUT /T for the main wait, this way you can skip it and initiate a restart immediately
TIMEOUT 10720
REM 3 hours minus 60 seconds to allow for 60 second restart notification
cls
echo server will restart
TIMEOUT /T 60
cls
goto :start
(Since you seem to be somewhat new to batch, REM is used to comment out lines)
To answer your question surrounding why the :choice section is a thing:
It is initiated after the :start section, allowing you to either (Y) restart, or (N) stop the server

Make a batch-file loop when there is internet or launch a program and exit when there is NO internet

The idea is to have a batch file ping the internet every 5 minutes to see if there is a connection. If there is an internet connection the batch file will loop every 5 minutes to ping again. If there is NO internet connection then the batch file will launch a program and exit. I created a batch file that does everything but exit when the internet is not detected.
What I have so far loops just the way it should but I'm stuck at making the file exit if the internet connection is NOT connected and notepad.exe is launched. I don't know much about batch-files and am trying to piece things together from searches, I need help.
#echo off
setlocal
cls
:loop
#ping 209.222.18.218 -n 1 -w 1000> nul
if %ERRORLEVEL% EQU 1 start C:\windows\notepad.exe
timeout /t 60 >null
goto loop
Expected result: the batch file pings the internet every 5 minutes to detected either there is or isn't an internet connection. If there is an internet connection the batch file will loop every 5 minutes. If there isn't an internet connection the batch file will launch notepad.exe and then exit itself.
Actual result, I get the batch file to loop when it detects internet but I can't get it to exit itself when there is NO internet.
Rearrange your lines a bit:
:loop
timeout 300 >nul
ping 209.222.18.218 -n 1 -w 1000 |find "TTL=" >nul
if %errorlevel%==0 goto :loop
REM if not errorlevel 1 goto :loop
start C:\windows\notepad.exe
Use one of the if lines - whichever you are more comfortable with.

batch file timeout command jumping to 10k, 30k, 40k seconds

I'm currently running a set of commands that utilizes the timeout function in my batch files. The timeout function uses a variable for how many seconds it should pause, and I think that may be causing issues, but I desperately need it to function properly.
I've got an update to my question which details more about what may be happening.
I used some suggestions that I had in the comments to try and fix my script, but it did not work.
Here is my script:
:connectToTheInternet
setlocal EnableDelayedExpansion
set timeout=2
echo checking internet connection
echo.
:connectToTheInternetRestart
ping -n 1 google.com | find /i "TTL=" >NUL && (
echo Internet connection already established, checking script versions.
echo.
goto scriptVersion
REM toupd
) || (
if "%COUNTER%"=="0" (
echo Creating wifi profile.
echo.
set /A COUNTER=1
goto createWifiProfile
)
SET /A "tries = (%COUNTER%-1)+1"
REM if failed endonfail times, give up
if "%COUNTER%"=="%endonfail%" (
echo Error: Could not connect to network %tries% times, stopping script.
echo.
goto endNoShutdown
)
echo.
echo Attempt #%COUNTER%.
echo.
REM raise waiting time between connections
SET /A "modulo = (%COUNTER%-1) %% 2"
if "%modulo%" EQU "0" (
set /A timeout=timeout+2
echo failed %tries% times in a row. Increasing wait time between actions to %timeout% seconds.
set /A COUNTER=COUNTER+1
) else (
set /A COUNTER=COUNTER+1
)
REM disconnect existing network
netsh wlan disconnect
netsh wlan delete profile name="%wifissid%"
timeout /t 1 /nobreak
echo.
REM attempt connection
netsh wlan add profile filename="connect.xml"
netsh wlan connect name="%wifissid%" ssid="%wifissid%" >NUL
echo.
echo Wait time is currently !timeout! seconds.
timeout /t !timeout! /nobreak
echo.
REM check pings
ping -n 1 google.com | find /i "TTL=" >NUL && (
echo Successfully connected. Checking script version.
echo.
goto scriptVersion
) || (
set /a COUNTER2=COUNTER+1
echo Connection attempt failed. Starting attempt #%COUNTER2% in 3 seconds.
echo.
timeout /t 3 /nobreak
cls
goto connectToTheInternetRestart
)
)
My problem is specifically the timeout /t !timeout! /nobreak which should wait for a variabled amount of time. The problem is, sometimes (it only happens when the device successfully connects to the internet, but not every time the device connects to the internet) the timeout jumps up to 10,000 seconds, 30,000 seconds or some other random high number (And they really are random, I've seen some at like 12,536 seconds.) When it should always be less than 12 based on the timeout variable. Yes, I mean seconds NOT milliseconds. As you can imagine, there is a big difference between 10k seconds (roughly 21 days) vs 10 seconds.
I have no idea what could be causing this, or how to solve it and would love some help.
Here's a left-field thought, brought about by your comment
(it only happens when the device successfully connects to the internet, but not every time the device connects to the internet)
Before it connects to the internet, is the date, time and timezone correct? Connecting will synchronise the time - which may muck up your timeouts!
Like I said: left-field, but...
choice seems not to be affected by the mentioned behaviour of timeout: "when changing the time from another cmd window while running a timeout 300, the "waiting for ..." prompt indeed changes the remaining time accordingly".
So instead of timeout /t !timeout! you can use
choice /n /c yn /d y /t !timeout!
/c yn is just for making it language independent.

Use a batch file to send a command to another batch file

I'm currently running a StarMade Server and am working on having the server auto restart.
Right now we have 2 batch files, 1 that runs the server, and another to restart the server.
The batch file running the server is:
title Main Starmade Server
java -Xms4096m -Xmx6144m -XX:PermSize=512m -XX:MaxPermSize=1024m -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+UseNUMA -XX:+CMSParallelRemarkEnabled -XX:MaxGCPauseMillis=50 -XX:+UseAdaptiveGCBoundary -XX:-UseGCOverheadLimit -XX:+UseBiasedLocking -XX:SurvivorRatio=8 -XX:TargetSurvivorRatio=90 -XX:UseSSE=3 -XX:+UseLargePages -XX:+UseFastAccessorMethods -XX:+UseStringCache -XX:+UseCompressedOops -XX:+AggressiveOpts -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintTenuringDistribution -Xloggc:memory.log -Xincgc -Xshare:off -jar StarMade.jar -server
EXIT
And the batch file restarting the server is:
#echo off
title Server Automated Restarter
:start
echo Starting Starmade Server
set time=60
set timer
cd C:\StarMade\StarMade
start StartServer.bat /REALTIME
timeout 120
echo Server Started
timeout 5
:loop
cls
IF %time% GTR 0 (
set /a time=%time% -1
set /a min=%time%/60 +1
echo Server Restarter Active
echo Next Restart In %time% Seconds; Less Than %min% Minutes Remaining
cd C:\Windows\System32
ping (my ip goes here, but I've removed it for this post) -n 2 > NUL
goto loop
)
echo Killing Java Task
timeout 5
taskkill /f /im java.exe
timeout 30
echo Restarting Starmade Server
cls
goto start
Obviously, this technically crashes the server is it is force stopped. The game has a "/force_save" command and a "/shutdown (timeInSec)" command. I would like to be able to use the automated restarter bat to send those commands to the other batch file which is running the server. While the server is running, no other commands and be called within the batch file running the server (unless manually typed).

Run Batch File That Stops Process at Specified Time

I'm running a batch file to start a Minecraft server. Here is the following script:
#echo off
"%ProgramFiles%\Java\jre7\bin\java.exe" -Xms1024M -Xmx1024M -jar craftbukkit-1.4.7-R1.0.jar
pause
It just occurred to me that perhaps its possible for the batch file to automatically stop the process on a specified time of day, but not through a countdown of any sort.
In addition, I had hoped that it can write the following lines in the command prompt:
say Server is closing in 15 minutes (1:45am)
say Server is closing in 10 minutes (1:50am)
say Server is closing in 5 minutes (1:55am)
say Server is closing, goodbye. (2:00am)
stop (2:00am)
If this is not possible, any recommendation would be appreciated.
Is this what you're looking for?
#echo off
setlocal enabledelayedexpansion
cmd /c "%ProgramFiles%\Java\jre7\bin\java.exe" -Xms1024M -Xmx1024M -jar craftbukkit-1.4.7-R1.0.jar
:waitloop
for /f "tokens=1-3 delims=:." %%I in ("%time%") do (
if "%%I"==" 1" (
if "%%J"=="45" (
echo ! Server is closing in 15 minutes.
) else if "%%J"=="50" (
echo ! Server is closing in 10 minutes.
) else if "%%J"=="55" (
echo ! Server is closing in 5 minutes.
)
) else if "%%I"==" 2" (
echo ! Toodloo.
wmic process where name="java.exe" delete >NUL 2>NUL
goto :EOF
)
set /a secs=60 - %%K + 1
ping 127.0.0.1 -n !secs! >NUL
)
goto waitloop

Resources