2

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.

Community
  • 1
  • 1
  • Note that I remember there for sure IS a tool to terminate specific JAR application in Java. I will tomorow update my post to mention it - I hae script that uses it at work. But I'm not sure whether it solves my problem. – Tomáš Zato - Reinstate Monica Mar 30 '16 at 23:15
  • You could just bind to a specific port when the server starts. That would throw an exception if you tried to bind to a port that was already bound to, so you would only have one instance running at a time. – satnam Mar 30 '16 at 23:27
  • @rgettman Can you explain how this solves my problem? I have multiple java.exe processes running, only one of them is the server. – Tomáš Zato - Reinstate Monica Mar 31 '16 at 00:32
  • @satnam yeah, but the outpur in the console would not be so nice and readable for other users, such as my little sister, who might wanna run the server while I'm at work. – Tomáš Zato - Reinstate Monica Mar 31 '16 at 00:36
  • @rgettman Please not that the answers in the other questions don't even **mention** java. – Tomáš Zato - Reinstate Monica Mar 31 '16 at 00:44
  • @TomášZato While this might not quite be what your looking for, it should work for the most part: http://pastebin.com/cvH7VWP4 – Preston159 Mar 31 '16 at 00:47
  • @Preston159 I thought of that but the probability of this failing is rather high. Even after adding deletion of the `running` lock, the file still will not be deleted if the server window is closed, computer rebooted while server running etc. If this was the best SO can give me, I'd rather write CScript regex to simply parse list of running processes. – Tomáš Zato - Reinstate Monica Mar 31 '16 at 00:51
  • @TomášZato I thought of that as well, but with the example you gave of why you'd need something like this, it seemed like a viable option. I understand it's not ideal, though. – Preston159 Mar 31 '16 at 00:52
  • _close everything crowd_ thanks you for the note. Also [this question](https://stackoverflow.com/questions/162291/how-to-check-if-a-process-is-running-via-a-batch-script) might help, assuming the process name is static. – Bloodied Mar 31 '16 at 02:03
  • @Arescet Thanks for that suggestion, but I already mentioned I am aware of that option and I am saving it as a last resort. I will do that if I fail to find the answer even after digging all over the java bin folder – Tomáš Zato - Reinstate Monica Mar 31 '16 at 02:05
  • @TomášZato perhaps [this](https://stackoverflow.com/questions/28785141/how-to-know-if-a-jar-file-is-already-running)? If the java file used to host the server is by you, a pre-function can be added to check if it's already running from java itself.. – Bloodied Mar 31 '16 at 02:09
  • @TomášZato, you can make the console output nice very easily. Just use a TRY CATCH around your code that binds the server to the port. Then if there is a socket binding issue, instead of printing a stack trace, just say "The server is already running!" – satnam Apr 01 '16 at 02:44
  • @satnam I don't think recompiling the server is viable option, especially for future readers who ask about different applications. – Tomáš Zato - Reinstate Monica Apr 01 '16 at 09:10

1 Answers1

0

So I found out that there's this jps utility which conveniently lists running java processes and their arguments. I meantioned in the comments I think there's such a thing, but it took a while to dig out the proper use. I created following script:

@echo off
setlocal enabledelayedexpansion
echo Checking for %1
for /f "tokens=1" %%i in ('jps -m ^| find "%1"') do (
  echo Jar already running
  goto end
)
echo Jar not running
:end
pause

Usage:

> isjarrunning.bat something.jar

The name is case-sensitive!

In order to use this script in IF statements, it must be tweaked:

isjarrunning.bat

@echo off
IF [%1] == [] (
  echo Invalid parameter. Usage:
  echo     isjarrunning.bat NameOfTheJarFile.jar
  exit /B 2
)
for /f "tokens=1" %%i in ('jps -m ^| find "%1"') do (
  exit /B 1
)
exit /B 0

Usage in your scripts:

testjarrunning.bat

@echo off
call isjarrunning.bat MyProgram.jar
echo Error level: %errorlevel%
if errorlevel 1 (
   echo Jar file already running!
   exit /b 1
) ELSE (
   echo Starting jar file
   start javaw -jar path\MyProgram.jar
)
echo Ende
Community
  • 1
  • 1