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
Related
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
#echo off
setlocal EnableDelayedExpansion
color b
goto play
:play
cls
set name2= OoggieBoogie
echo Hello, My name is !name2!^^! I'm an AI. I'm here to help with your lazy
Butt :D^^!
timeout /t 3 >null
echo!name2!: May I Have Your Have Your Name Please? :)
color c
echo (Pssst^^! Want to cut the Bullshit and go straight in? Select "Express" please!)
timeout /t 2 >null
echo A. My name is
echo B. Express
set /p input=
if !input! equ B goto Writing2
cls
echo!name2!: Hello !name!, Shall we continue now?
echo 1.Yes :D
echo 2.No -_-" ..
set /p input=!name!:
if !input! equ 1 goto Writing
if !input! equ Yes goto Writing
if !input! equ 2 exit
if !input! equ No exit
:Writing2
echo Okay.. Whatever you want Damn.. I was trying to be nice ^^!
echo Anyway. Inatiating EXPRESS Route----->
goto Writing
Hello!
What I am trying to do here is skipping all the steps and go straight to "Writting2" if typed Express.
I am almost done with this fun program but I can't figure out a good way skip all the steps.
When I type "Express or select 'B'
it crashes.
but If I write a name the program works as usual!
Thanks in Advance!
Sorry in Advance if I did something wrong in the community.
This is a way you can do what you want:
#echo off
color b
goto play
:play
cls
set name2= OoggieBoogie
echo Hello, My name is %name2% I'm an AI. I'm here to help with your lazy
::echo Butt :D^^!
timeout /t 3 >nul
echo %name2%: May I Have Your Have Your Name Please? :)
color c
echo (Pssst^^! Want to cut the Bullshit and go straight in? Select "Express" please!)
timeout /t 2 >nul
echo A. My name is
echo B. Express
choice /c:AB>NUL
if errorlevel 2 goto Writing2
set /p "name=Enter your name: "
:Writing
cls
echo %name2%: Hello %name%, Shall we continue now?
echo 1.Yes :D
echo 2.No -_-" ..
choice /c:1Y2N>NUL
if errorlevel 4 goto exit
if errorlevel 3 goto exit
if errorlevel 2 goto Writing
if errorlevel 1 goto Writing
:Writing2
echo Okay.. Whatever you want Damn.. I was trying to be nice ^^!
echo Anyway. Inatiating EXPRESS Route-----^>
goto Writing
:exit
exit /b
Remember: Variables can be accessed by %variable_name% and you can set them by set "variable_name=variable_value" as #Compo mentioned above.
It is better to use choice /c option in your future batchfiles. A disadvantage of this option is that you cannot enter a string with more than 1 character, but it does it's own errorhandling, so you don't have to deal with invalid responses. Also, when you write
echo Anyway. Initiating EXPRESS Route----->
> symbol causes problems as it is a redirection character and should be escaped:
echo Anyway. Initiating EXPRESS Route-----^>
I am new to this and I am trying a basic example I saw on Youtube:
#echo off
echo this little program helps you to refresh a website
pause
cls
echo but first we have to do some preferences
pause
cls
echo please enter the website name here
set /p website=
cls
echo please enter the time between refreshes
set /p time=
cls
echo Set limited or unlimited refresh
set /p refresh=
if'%refresh%'=='1' goto :once
if'%refresh%'=='0' goto :unlimited
cls
:once
cls
echo Preferences finished
pause
timeout /t %time% /nobreak
start %website%
exit
:unlimited
cls
echo Preferences finished
pause
:again
timeout /t %time% /nobreak
start %website%
goto :again
This code is supposed to ask a user for a website and a refresh time and it will refresh that website by reopening it in IE (Internet Explorer), however I have 2 problems with this:
The loop does not work if I enter 0 or '0' it will only execute once and exit the .bat
Is there a way to refresh a page without reopening the page in a new tab, for example just refresh the current tab ?
There are some syntax errors in your script.
EXIT command
EXIT exits the script. You may want to change that to something else.
IF Statement
if'%refresh%'=='1' goto :once
is slightly incorrect, as there is a missing space between if and the comparee. In addition, using double quotes ensure it to successfully compare with values including spaces.
if "%refresh%"=="1" goto :once
Undefined Input
If variable %refresh% is not 1 or 0, it goes to :once and execute the code there.
Refreshing Webpage
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate("Chrome")
WshShell.SendKeys "{F5}"
Save this script as Refresh.vbs and put it in the same folder as the batch file.
Then, when you want to refresh the webpage, do cscript //nologo Refresh.vbs.
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.
Whenever i run the .bat file it all goes well i choose my time it says it in minutes and all until it comes to confirming your decision and i really have no clue how to fix it for some reason it just goes to :shutdown
Intention: I am using this as a simple little tool when installing games for example if i have to leave steam to download and install a large game overnight i just choose a time and leave it :)
Note: I have no experience with coding ( except changing values on LUA scripts in games ) so an explanation would be
Greatly Appreciated and yes sure i can just use the shutdown command but i wanted to create something a little more fancy
Also if someone could explain to me what the
/s in shutdown
and /a in set timedelay commands do because as i stated i have little to no experience
.
If you can't answer or can't be bothered thank you for your time either way!
Thank you in advance! :)
.
#echo off
cls
title Delayed Shutdown
echo Delayed Shutdown by Martin Angelov
echo Press Any Key To Choose Delay
Pause>NUL
:choosedelay
cls
echo Type in the desired ammount of delay
set /p timedelaysec=
set /a timedelaymin=%timedelaysec% / 60
:confirmation
echo Your current desired shutdown time is:
echo %timedelaysec% Seconds
echo ( %timedelaymin% Minutes )
echo Press 1 to Confirm Shutdown
echo Press 2 to Change Delay
echo press 3 to Exit Program
set /p confirmaation =
if "%confirmaation%" == "1" goto shutdown
if "%confirmaation%" == "2" goto choosedelay
if "%confirmaation%" == "3" goto exit
:shutdown
cls
echo Shutting Down!
echo Delay Chosen:
echo %timedelaysec% Seconds
echo ( %timedelaymin% Minutes )
pause
exit
:exit
cls
echo Exiting Program...
ping 0.0.0.0 -n 2
exit
:Temporary so it doesn't actually enable the shutdown
shutdown /s /t %timedelaysec%
In batch files, it's usually something simple and frustrating.
Try
set /p confirmaation=
(Note that I removed a space before the equals.)
If that fixes your problems, your environment variable wasn't set to any of the three values you checked for and the logic very naturally went line-by-line to :shutdown.
Good luck!