问题
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.
回答1:
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
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/26250361/need-auto-restart-script-in-batch-for-minecraft-server