Need auto-restart script in batch for minecraft server - windows

I am currently an administrator on a private Minecraft server, though in this case the technical question lies outside the scope of typical minecraft supoort.
I wish to have the batch file that launches the server restart at 12 am and 12 pm, though I have little experience in batch and a cursory google search brings up nothing helpful.
The issue I run into is both that I have no idea if batch CAN execute commands within a java server console, send the commands to save the server and then exit, and restart itself, due to only knowing basic batch functions.
More specifically, I want the batch file itself to run a command in the server window after either 43200 seconds or on each of the 12s, then restart itself. I do not know how to get a batch file to run a command within the server command line, or if it's even possible.
The current batch code is as follows:
#echo off
:Minecraft
echo (%time%) Minecraft started.
java -Xms2048m -Xmx2048m -XX:PermSize=128m -jar FTBServer-1.6.4-965.jar nogui
pause
echo (%time%) WARNING: Minecraft closed or crashed, restarting.
ping 1.1.1.1 -n 1 -w 3000 >nul
goto Minecraft
Any help would be aprreciated. Thanks.

i use this but if you want it to restart it automatically then just delete the :choise part and make a loop from start to restart
#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
ps. replace minecraft_server.1.8.3.jar with the name of your server file

Solution 1:
I would suggest to use the windows task scheduler instead of a batch file. There you can create a task, schedule it to be triggered at 12am/pm and insert any cmd command you want to be executed. However, it's non-trivial to cummunicate with the server console without knowing the specific interface or how to administrate a minecraft server. What you can do is simply kill the server and restart it using the command line.
Solution 2:
If you don't like this solution and don't know how to communicate with the server console you can try this:
Take a look at AutoIt (https://www.autoitscript.com/site/). It's a VERY simple script language which also can simulate click and input from the keyboard. So you can write a script that sets the focus to your server console and types the desired command to restart the server. This AutoIt script can be compiled to an exe file or you can run it as an au3 script.
You should still use the task scheduler to run your exe/script at 12am/pm.
If you need some help writing the AutoIt script I can help you with that.

I wrote a similar program for a friend in AutoIt here is the script i commented the lines you need to config:
HotKeySet("{ESC}", end)
HotKeySet("{F1}", start) ;optional
HotKeySet("{F2}", pause) ;optional
pause() ; starts the pause loop when started
; restarts the server all 12 hours
Func start()
$Path = "PathToYourBatch.bat" ; self explained
While 1
If #HOUR = 00 Or #HOUR = 12 Then ;starts the server at 00 and 12
Run($Path)
EndIf
WEnd
EndFunc
Func pause()
While 1
Sleep(500) ; waits 500 ms to reduce lag
WEnd
EndFunc
Func end()
Exit
EndFunc
You dont need to use the hotkeys but you could easily control the program with them(remote desktop)
You can use a online compiler like (http://www.script-example.com/themen/AutoIT-Online-Compiler.php) or download it from (https://www.autoitscript.com/site/) hope i could help if any further questions with the code ask me.

Related

Auto restart batch file upon exit from batch

Currently I have a batch file running:
cd "C:\Users\chriscrutt\Desktop\"
:loop
Start /w yeet.bat
goto loop
This runs "yeet.bat". Sometimes the command prompt will crash but I need it to automatically restart. That is why I used "/w" but it requires me to manually input "N" when it asks: Terminate batch job (Y/N)? Is there a way to make it so it automatically restarts or so it will automatically say "N" to restart it?
This is how it goes. I run the bat file which runs the yeet file which runs the other program. the code for "yeet.bat" is
title ETHBTC
cd "C:\Users\chriscrutt\Google Drive\gekkoETHBTC"
node gekko
When i do "node gekko" it runs the bot.
This code do what you want: each time that yeet.bat ends for any reason it is restarted again "immediately and automatically".
cd "C:\Users\chriscrutt\Desktop\"
:loop
Start yeet.bat | set /P "="
goto loop

In Windows, how would I create a batch file that opens 2 command windows with a specific Java program?

I am trying to create a batch file that will reopen my KnockKnockClient and KnockKnockServer programs , get them started.
So far , my batch file looks like this :
#echo off
setlocal
start cmd
start java KnockKnockServer
ping 192.0.2.2 -n 1 -w 5000 > null
start /wait java KnockKnockClient
if errorlevel 1 goto retry
echo Finished successfully
exit
:retry
Also, suppose that I want it to restart so that the CMD window with the KnockKnockClient is active. Is that possible ?
any tips appreciate, thank
"start" itself invokes cmd.
you don't need to give "start cmd"
Maybe your directory path should be varied. (locate correct path of your program)

How to close Windows console automatically (send CTRL+C to console programatically)?

I have two windows batch scripts, script1.bat and script2.bat. script2.bat is launched from script1.bat.
script1.bat:
...
start call script2.bat
...
script2.bat has to be closed when user closes script1.bat's console (in another words, script2.bat and its console should be closed automatically when script1.bat is closed). But script2.bat should not be killed, It should be terminated because script2.bat has to release database connection. I mean unix signal teminology by using kill and terminate words. So scrip2.bat should not be killed immediately, it should be terminated in way that allows the process to perform nice termination releasing resources and saving state if appropriate.
I made it for unix system and I resolved it as following.
script1.sh:
...
sh script2.sh > /dev/null 2>&1 &
script2_pid="$!"
trap 'kill -15 $script2_pid' EXIT HUP TERM INT KILL
...
How to make it on Windows?
====EDIT====
I think that my question is not entirely clear for everyone so I would like to clarify it.
I have java application which connects to JBoss application server. It is tested now and I need to launch both in very convenient way. I prepared batch script (script1.bat)which starts both, client application and JBoss application. This script also do another things like setting environment variables. So script2.bat is a Jboss standalone.bat file in fact. I wouldn't like to edit this file.
script1.bat is my script, it sets environment variables, start JBoss and start my java application.
My script (script1.bat):
...
set environment variable
...
REM start jboss
start standalone.bat
REM start my java application
java ...
When user closes Java application, JBoss should also be closed. I need it only for tester's convenient and I know that in production environmnet it should work in another way.
I don't know how to terminate JBoss automatically after user closes my java client application. JBoss connects to H2 database and creates lock on it so if JBoss will be killed immediately then database lock is still there. If JBoss process receives CTRL+C it terminates properly (removes database lock). I want to make it automatically, After user closes java client application, JBoss has to be closed as it receives CTRL+C.
I have no idea how to do it on Windows. But I did it on linux and I added my code to this question.
Sorry, but your script makes no sense. start call script2.bat?? The combination of start and call is nonsense. You use either one or the other but not both.
Further I have to disappiont you. There is no way to close a console but keep the code running. You can't even start a hidden console in Windows. Closing the console will instantly stop the execution of the code.
EDIT:
Now I understand what you are asking. Start your first script using
start "somename" script1.bat
This will start script1 in a console with "somename" as window title.
In the second script you can use this:
:WORKING
REM Do some stuff here
REM and here
REM and here
FOR /f "tokens=*" %%A IN ('tasklist^ /v^| findstr /i /c:"somename"') DO GOTO WORKING
REM shutdown part
REM close db connections
REM exit script2
This part will check if the a console with "somename" in the title is running and if yes reenter the loop. At the end of the loop it alway checks whether script1 is still active. As soon as it's not found the code goes on to the shutdown part.
If you only want to wait for script1 to terminate you can use PING -n 5 127.0.0.1 > NUL as a 5 (or longer, just replace the 5 ^^) second timeout to avoid busy-waiting:
:WORKING
PING -n 5 127.0.0.1 > NUL
FOR /f "tokens=*" %%A IN ('tasklist^ /v^| findstr /i /c:"somename"') DO GOTO WORKING
REM shutdown part
REM close db connections etc.
REM exit script2
If CLI is enabled in JBoss then JBoss can be shutdowned by CLI. Following batch shows how to shutdown JBoss
...
set environment variable
...
REM start jboss
start "UNIQUE_NAME" path_to_JBoss\bin\standalone.bat -c standalone.xml
REM start java application
java ...
REM shutdown JBoss by CLI
call path_to_JBoss\bin\jboss-cli.bat --connect ":shutdown"
REM it is just in case. If CLI did not shutdown JBoss, then following line kills JBoss without release of resource.
FOR /f "TOKENS=1,2,*" %%a IN ('tasklist^ /FI "WINDOWTITLE eq UNIQUE_NAME*" /nh') DO taskkill /pid %%b

How to lock the computer after executing an exe file using batch file programming in windows?

I have a batch file program something like.
#echo off
start Mat.exe
>>Need a code here to runand check for termination of "Mat.exe"
rundll32.exe user32.dll, LockWorkStation
>>end of program
If anyone can help me set up the program so that I can lock my computer as soon as the "Mat.exe" file is terminated. I'd really appreciate it. Thanks in advance
Simply drop the start before Mat.exe so your batch-file will wait until Mat.exe has finished.
Edit: This only works if your Mat.exe runs in a console or similar.
If dropping the start didn't work you may have to check wheather or not Mat.exe is still running. For that I found a quite useful post, which should work for you: How to wait for a process to terminate to execute another process in batch file
Edit2: Complete Code:
#echo off
:LOOP
start Mat.exe
TASKLIST 2>&1 | find "Mat.exe" > nul
IF ERRORLEVEL 1 (
rundll32.exe user32.dll, LockWorkStation
) ELSE (
SLEEP 3
GOTO LOOP
)
You can adjust the SLEEP as you like, but i would suggest use at least 1 sec, otherwise you'll have 100% usage.
Try using the Start command with the /Wait switch. Here is an example:
#echo off
REM //start Mat.exe
start /wait [path of Mat.exe]
rundll32.exe user32.dll, LockWorkStation
Remember to use the 8.3 filenames and do not include (" ") in the path string

How to continuously execute an application with CMD?

I need to execute a synchronizing application continuously with a batch file. What I want to do is something like:
while true
execute application
sleep 1 minute
end while
Can this be done with CMD?
Thanks in advance.
Using pure batch you could do this:
:LOOP
yourprogram.exe
ping 127.0.0.1 -n 61 >NUL
goto :LOOP
This means you don't have to install any other programs or create more scripts. It might not be pretty but it works! I have pinged 61 times as pinging the loopback seems to create a delayed second.
Download unixutils for win32. In here, you will find sleep.exe. Put it somewhere in your path. (frex, in c:\windows)
Then, you can build a batch file similar to this
echo off
:here
sleep 60s
echo Exec app
rem your app goes here
goto here

Resources