java/shellscript code to find out if a jar file is already running on current machine

前端 未结 2 1849
-上瘾入骨i
-上瘾入骨i 2021-01-03 02:44

How do I find out if a specific JAR file is running on my machine or not? I just want to ensure that a specific JAR is only executed once at any time-- so if another request

2条回答
  •  走了就别回头了
    2021-01-03 03:09

    jps is a simple command-line tool that prints out the currently running JVMs and what they're running. You can invoke this from a shell script.

    jps -l will dump out the JVM process id and the main class that it's executing. I suspect that's the most appropriate for your requirement.

    Noting your comment re. jps being not supported, if it's a valid worry that you can't easily mitigate via testing when you upgrade a JDK/JRE, then perhaps use something like:

    pgrep -lf java
    

提交回复
热议问题