For the close everything crowd: This is not a duplicate of the question How to check if a process is running via a batch script because there may be multiple java.exe
processes running (as they are in my case) and I need only to check for the specific jar. That's why I have written jar java application in the title.
I have a "Minecraft server" icon on my desktop that starts Spigot minecraft server. I use such run.bat
file to do the job:
:start
java -Xmx1024M -Xms1024M -jar spigot-1.8.7.jar
pause
goto start
It's infinite loop to make server restarting simpler. Now it sometimes happens that I, or somebody else who's using the computer, try to start the server while it's already running. I would like to prevent this from happening. Something along:
IF spigot_server_running (
echo The server is already running. If you can't connect, try to check the console window for errors.
echo if all else fails, terminate the java process from task manager.
pause
exit 1
)
What can I do to detect it? I guess list of running processes can be parsed similarly to what Linux's grep
does. But isn't there some command line tool for this distributed along with java JRE or JDK? I wouldn't install extra program for this task but I would prefer official Java API. But remember it must work with batch or Windows CScript files.