'Identify wether program is still running in Batch file
I currently have a batch file that automates 2 game servers and 2 Discord Bots and 1 Discord Webhook. The batch currently works the folowing way. Both Servers must start at different times. Thus the need to check the time constantly as well as checking their status.
Edit: If there are any ways to make my batch file simpler or more efficient do let me know.
Edit: I am planning on later on porting this as similar as possible on to Linux but with a few aditions as the servers will be hosted on a cluster composed of 2 Raspberry Pi's 4 Model B 8GB versions and 1 Raspberry Pi 3 for this program, Discord Bots and Webhook.
Batch Starts> Checks Time> Starts first Server, 2 Discord bots and sends a message through the webhook> Checks Time Again> Starts other server and sends another message through the webhook> Checks Time Again Closes First Server Only and sends message through the webhook> Checks Time Again> Closes Second Server, Discord Bot's, Sends message through the webhook> Restarts the batch.
That used to be fine but one of the servers is a modded game server and sometimes there can be instabilities that cause it to crash due to how the mods try and work and the RAM limitations imposed by the 32bit software (64 Bit version is not released yet).
I would like to be able to make it so the batch checks constantly if the servers are open in the following way if possible (I have the Idea Just dont know how to make it Work):
Server 1 Starts > Check if ONLINE> If succesful check time> Check again if Online> If Time = Next Server 2> Start Server 2> Check Server 1 & 2> Check Time> Check Servers Again> If Time = Next> Stop Server 1> Check Server 2> Check Time> Check Server Again> Check Time> If Time = Next Stop Remaining Programs> Restart Batch File.
If Check = OFFLINE > Excecute Start Server # Command > Proceed with rest of commands.
I am currently an amateur at best but this is what I have as my base Batch at the time of writing this.
Notes: I use the Call feature I use it multiple times to call batch files for short commands only like sending the messages through the Webhook and a batch file of a Percentage Bar.
@ECHO OFF
CALL progressbar 10
:LOOP
SET hour=%time:~0,2%
IF %hour% GEQ 9 IF %hour% LEQ 10 (GOTO RUNIT)
ECHO Wrong time to start!
timeout /t 15
GOTO LOOP
:RUNIT
ECHO Normal Server and Bot Started!
CALL MessageBot 1
CALL progressbar 20
cd "D:\Servers\Bots\Discord\Bot-1"
start DiscordBot.lnk
cd "D:\Servers\Bots\Discord\Bot-2"
start DiscordBot2.lnk
cd "D:\Servers\Normal Server"
start MinecraftServerLatest.lnk
cd "D:\Servers\Starter"
CALL progressbar 30
:LOOP1
SET hour=%time:~0,2%
IF %hour% GEQ 12 IF %hour% LEQ 13 (GOTO RUNIT1)
ECHO Wrong time to start!
timeout /t 15
GOTO LOOP1
:RUNIT1
ECHO Modded Server Started!
cd "D:\Servers\Starter"
CALL progressbar 40
CALL MessageBot 2
cd "D:\Servers\ModLoader"
start ModdedServer.lnk
cd "D:\Servers\Starter"
CALL progressbar 50
:LOOP2
SET hour=%time:~0,2%
IF %hour% GEQ 15 IF %hour% LEQ 16 (GOTO CLOSEIT)
ECHO Wrong time to stop!
timeout /t 15
GOTO LOOP2
:CLOSEIT
CALL progressbar 60
for /f "useback tokens=* delims=" %%# in (
`wmic process where "CommandLine like '%%server.jar%%' and not CommandLine like '%%wmic%%' " get ProcessId /Format:Value`
) do (
for /f "tokens=* delims=" %%a in ("%%#") do set "%%a"
)
echo %processId%
taskkill /F /PID %processId% /T
ECHO Normal Server Closed!
CALL MessageBot 3
:LOOP3
CALL progressbar 70
SET hour=%time:~0,2%
IF %hour% GEQ 19 IF %hour% LEQ 20 (goto CLOSEIT1)
ECHO Wrong time to stop!
timeout /t 15
goto LOOP3
:CLOSEIT1
CALL progressbar 80
for /f "useback tokens=* delims=" %%# in (
`wmic process where "CommandLine like '%%ModdedServer.exe%%' and not CommandLine like '%%wmic%%' " get ProcessId /Format:Value`
) do (
for /f "tokens=* delims=" %%a in ("%%#") do set "%%a"
)
echo %processId%
taskkill /PID %processId%
ECHO Moded Server Closed!
CALL MessageBot 4
CALL progressbar 90
timeout /t 360
for /f "useback tokens=* delims=" %%# in (
`wmic process where "CommandLine like '%%DiscordBot1.cmd%%' and not CommandLine like '%%wmic%%' " get ProcessId /Format:Value`
) do (
for /f "tokens=* delims=" %%a in ("%%#") do set "%%a"
)
echo %processId%
taskkill /PID %processId%
for /f "useback tokens=* delims=" %%# in (
`wmic process where "CommandLine like '%%DiscordBot2.cmd%%' and not CommandLine like '%%wmic%%' " get ProcessId /Format:Value`
) do (
for /f "tokens=* delims=" %%a in ("%%#") do set "%%a"
)
echo %processId%
taskkill /PID %processId%
ECHO Discord Bot's Stopped!
CALL progressbar 100
cd "D:\Servers\Starter"
start "TWOTW".bat
timeout /t 15
:END
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
