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).
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
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.
I have a script to stop and start IIS services where in this script, it will define the log and the directory. The created log is growing file, since it has the same name directory (code below):
#ECHO About to stop the StackProcess
cd %windir%\system32\inetsrv
appcmd stop apppool /apppool.name:StackProcess
echo %date% %time% - Service Stopped Successfully > C:\Logs\StackProcess_IISrestart.log
#ECHO StackProcess Stopped
TIMEOUT /t 05 /nobreak
#ECHO StackProcess
cd %windir%\system32\inetsrv
appcmd start apppool /apppool.name:StackProcess
echo %date% %time% - Service Restarted Successfully > C:\Logs\StackProcess_IISrestart.log
#ECHO StackProcess Restarted
My next step was to create a log file which will named according to datestamp like "StackProcess_IISrestart-YYYYMMDD" and this log file will growing also.
echo off
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set SUBFILENAME=StackProcess%CUR_YYYY%%CUR_MM%%CUR_DD%
#ECHO About to stop the StackProcess
cd %windir%\system32\inetsrv
appcmd stop apppool /apppool.name:StackProcess
echo %date% %time% - Service Stopped Successfully > C:\Logs\%SUBFILENAME%.log
#ECHO StackProcess Stopped
TIMEOUT /t 05 /nobreak
#ECHO StackProcess
cd %windir%\system32\inetsrv
appcmd start apppool /apppool.name:StackProcess
echo %date% %time% - Service Restarted Successfully > C:\Logs\%SUBFILENAME%.log
#ECHO StackProcess Restarted
But this code were only works where the file will overwrite the existing one, which means the the log file will not growing for the day. Is there anything I miss to include code when I want it grows instead of this? Help me, thanks.
If I understand correctly, it appears that you do not understand how the redirection operator works. > starts from new, >> appends. Simply changing to the appropriate operator will fix your issue.
#Echo Off
Set "log="
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
)Do If Not Defined log Set "log=StackProcess%%A%%B%%C.log"
Echo Stopping the StackProcess
"%__AppDir__%InetSrv\AppCmd.exe" Stop AppPool /apppool.name:StackProcess
Echo %DATE% %TIME% - Service Stopped Successfully>"%log%"
Echo StackProcess Stopped
Timeout 5 /NoBreak>Nul
Echo Starting the StackProcess
"%__AppDir__%InetSrv\AppCmd.exe" Start AppPool /apppool.name:StackProcess
Echo %DATE% %TIME% - Service Restarted Successfully>>"%log%"
Echo StackProcess Restarted
Timeout 2 /NoBreak>Nul
Please note, whilst I may have made some improvements in the example above, that you are logging 'successful' messages without actually verifying that the start and stop processes were successful. I'll leave it to you to decide if you wish to expand your code to cater for such verification.
I would like to create a .bat file that will give me option to chose between different servers and perform actions such as stop/start services. So far I am getting System error 67 has occurred.The network name cannot be found. Is there a better way to do this. or can I chose the server name from a pop up option.
#ECHO OFF
CLS
ECHO 1.server1
ECHO 2.server2
ECHO 3.server3
ECHO 4.server4
ECHO.
set /p server_name=Enter server name:
IF %server_name%== 1 GOTO app1
IF %server_name%== 2 GOTO app2
IF %server_name%== 3 GOTO app3
IF %server_name%== 4 GOTO app4
:app1
call:restart "server1"
GOTO End
:app2
call:restart "server2"
GOTO End
:app3
call:restart "server3"
GOTO End
:app4
call:restart "server4"
GOTO End
:restart
net use \\%~1/User:%username%
SC \\%~1 Stop service
timeout 10
SC \\%~1 Start service
GOTO End
Instead of debugging your file, I'd like to present you a different solution:
On the servers where you want to restart your services create this bat file:
#ECHO OFF
:STOP
SC STOP <your_service>
ping 127.0.0.1 -n 6 > nul
SC QUERY <your_service> | find /I "STATE" | find "STOPPED"
if errorlevel 1 goto :STOP
SC START <your_service>
Now, create on your servers tasks with the task scheduler. Don't set any triggers but make the task execute your newly created bat file. Select any options you need (like a specific user with a specific password, whether to run the script when no one is logged in or not, whether to run it with admin priveledges or not, etc.) and give it name (your_task).
Finally, modify your script like this:
#ECHO OFF
CLS
ECHO 1.server1
ECHO 2.server2
ECHO 3.server3
ECHO 4.server4
ECHO.
:SELECT
set /p server_name=Enter server name:
IF %server_name%==1 GOTO app1
IF %server_name%==2 GOTO app2
IF %server_name%==3 GOTO app3
IF %server_name%==4 GOTO app4
GOTO SELECT
:app1
SCHTASKS /RUN /S "server1" /TN "your_task"
GOTO End
:app2
SCHTASKS /RUN /S "server2" /TN "your_task"
GOTO End
:app3
SCHTASKS /RUN /S "server3" /TN "your_task"
GOTO End
:app4
SCHTASKS /RUN /S "server4" /TN "your_task"
GOTO End
:End
You can also pass a user and a password to the SCHTASKS command if needed.
It should be clear how it works. I'd recommend taking a closer look at the first bat file. Using this:
SC \\%~1 Stop service
timeout 10
SC \\%~1 Start service
might cause trouble. Sometimes service won't stop within 10 seconds out of various reasons. Trying to start the service while it hasn't stopped yet, will result in an error and the service will remain stopped.
Instead, our bat script within the scheduled task will try to stop the service, then wait for 5 seconds (yes, -n 6 means 5 seconds ^^) and then it will check whether the service has the state "STOPPED". If it is, it will start it, otherwise, it will re-try to stop it, wait 5 more seconds and so on.
I have around 140 virtual servers that need regular maintance, they all run message queuing, but the storage file needs to be kept below 512MB. I need to stop all the services in order and the last being MSMQ service, then go to the system32\msmg\storage file and delete all the *.mq files, once done I then would like the batch file to restart the services with the first being msmq. I also need to add some error messages, so if the service can not stop or start or its already running etc, below is what I think it should be like but not sure,. may be there is a better way of doing it,
the plan would be to but the batch file on all the servers and set a sceduled task to Run the batch file.
Would it be possible to have an email generated to say that it has been completed successfully ?
NET STOP AVLDataService 2>&1|FIND "2182"
IF errorlevel 1 goto :sub_already_stoped
You are on the right track. A batch file can certainly accomplish this with NET START/STOP calls in the desired order. For error handling, if the error is non-fatal echo a message to a text file and continue. If it's fatal, then use goto to jump to the end of the file. At the end of the file, you can email yourself the text file with error messages using BMAIL.
Try this:
#echo off
title Service Uninstaller
color 0A
set blank=
set service=blank
:start
echo.
echo.
echo.
SET /P service=Enter the name of the service you want to uninstall:
IF "%service%"=="" (ECHO Nothing is entered
GoTo :start)
cls
echo.
echo.
echo.
echo We will delete the service: %service%
ping -n 5 -w 1 127.0.0.1>nul
::net stop %service%
ping -n 2 -w 1 127.0.0.1>nul
sc delete %service%
pause
:end