Reputation: 4421
I've written a batch file that checks if only one instance of a server is running at any given time. The server is on a shared folder on a cloud, where multiple users have access to it.
If the server is running, a .txt file will be created, and as long as it's there, no one can start the server. When the server shuts down, the .txt file is deleted and another user is free to start it again.
minecraft_server.1.8.1.exe starts a Java process javaw.exe, which is the process that we need to monitor.
The code goes like this:
@echo off
IF EXIST *_RUNNING.txt (
echo "ERROR, SERVER ALREADY RUNNING as %computername%"
pause
EXIT
) ELSE (
copy NUL %computername%_RUNNING.txt
START /WAIT minecraft_server.1.8.1.exe
tasklist /FI "IMAGENAME eq javaw.exe" 2>NUL | find /I /N "javaw.exe">NUL
:loop
IF "%ERRORLEVEL%"=="0" (
TIMEOUT /t 60
GOTO loop
) ELSE (
del %computername%_RUNNING.txt
echo "Server ended."
pause
EXIT
)
)
Everything works except the loop. It keeps returning ") was unexpected at this time". I'm new to writing batch files so please help.
Upvotes: 0
Views: 88
Reputation: 80213
@echo off
IF EXIST *_RUNNING.txt (
echo "ERROR, SERVER ALREADY RUNNING as %computername%"
pause
EXIT
)
copy NUL %computername%_RUNNING.txt
START /WAIT minecraft_server.1.8.1.exe
:loop
tasklist /FI "IMAGENAME eq javaw.exe" 2>NUL | find /I /N "javaw.exe">NUL
IF "%ERRORLEVEL%"=="0" (
TIMEOUT /t 60
GOTO loop
)
del %computername%_RUNNING.txt
echo "Server ended."
pause
EXIT
Effectively, you have a label :loop
in a block (a parenthesised sequence of statements) in your original. Labels terminate blocks.
With these modifications, the unnecessary else
clauses have been removed. If batch exit
s or goto
s then the else
is not required - the next statements in the batch will be executed if the goto
/exit
doesn't happen.
Note that your label :loop
is in the wrong place. As it stood, errorlevel
would be established for the first and only invocation of tasklist
. Thereafter, the loop would perpetually find errorlevel
0 if it was set to 0 on the first instance. Moving it to the indicated location would execute tasklist/find
with a delay of 60 sec until errorlevel
became non-zero, when the non-goto path to terminate the procedure would be taken.
(not tested, of course...)
Upvotes: 1