'How do I run a .bat file for commands to execute in a second .bat file running a .jar?

Hey community I need to know how I can run .bat file to eacute a string of text to a .bat file running a .jar to use the command.

also the batch file to run the command in the other batch file will be run from Windows Task Scheduler.

This is for a Minecraft server by the way. anyhow below is the code used to run the .jar in the batch file. the command syntax is just "stop".


server.bat code

java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui.

So in long terms I need to make a batch file called stop.bat to be run from Windows Task Scheduler. In the stop.bat file I need the file to exacute the command in the server.bat file so that the server will stop, after that I can just run the server.bat to start the server again.

any thoughts on how this can be done? basic lay out how this should all work is below.

server-run.bat >>> "Runs" >>> minecraft-server.jar

Task Scheduler >>> "Runs" >>> stop.bat

stop.bat >>> "Executes command in" >>> server-run.bat



Solution 1:[1]

assuming your server-run.bat file looks something like this:

@echo off
:startserver
java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui.
:running

pause
REM or whatever you do normally to stop the server
:stopserver
java stop
REM or whatever the command to stop the server is
exit

for the file to be able to jump to some mark defined by acalling batch file, you should change it to:

@echo off
if "%*" NEQ "" goto %*
:startserver
java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui.
:running

pause
REM or whatever you do normally to stop the server
:stopserver
java stop
REM or whatever the command to stop the server is
exit

and stop.bat should look aproximately like this:

@echo off
start server-run.bat stopserver

remem^ber this is just another way of running the server-stop-command directly from stop.bat. if that wouldn't work (beacause it needs an environnement variable or something) this approach will also fail!

(this is beacause you can't directly interfere with a running batch file or its variables, you can only run a copy of it at the same time)

if you wnat to interfere with a running batch file, the easiest way would be to check periodically if a file exists:

@echo off
:startserver
if exists stopfile.txt del stopfile.txt
java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui.
:running

if exists stopfile.txt goto stopserver
timeout /T 2
goto running 


:stopserver
del stopfile.txt
java stop
REM or whatever the commabd to stop the server is
goto :EOF
exit

and stop.bat just does this:

@echo off
echo.something >stopfile.txt
exit

this will cause the server-running.bat to

-run the server

-check every 2 seconds if stopfile.txt apperared

-if it did, stop the server

(remember to change the dummy lines with the java code, i have no idea if they are right)

hope this helped in the 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
Solution 1 hammockdude