检查进程是否在windows/linux上运行

Var*_*run 9 java process

我有一个过程

Runtime rt = Runtime.getRuntime() ;
Process p = rt.exec(filebase+port+"/hlds.exe +ip "+ip+" +maxplayers "+players+ " -game cstrike -console +port "+port+" -nojoy -noipx -heapsize 250000 +map de_dust2 +servercfgfile server.cfg +lservercfgfile +mapcyclefile mapcycle.txt +motdfile motd.txt +logsdir logs -zone 2048",null,  new File(filebase+port)) ;
Run Code Online (Sandbox Code Playgroud)

我想继续检查这个进程是否正在运行或者在崩溃的情况下崩溃想要重新启动它,这个进程可以有多个实例,具体取决于端口

我可以在Linux和Windows上跟踪这个东西吗?阅读一些关于它的文章,但这个1有点不同,因为它涉及多次出现并且必须仅检查某个特定过程

Ali*_*aka 23

boolean isRunning(Process process) {
    try {
        process.exitValue();
        return false;
    } catch (Exception e) {
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

请参阅http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html#exitValue()


lob*_*234 3

您可以这样做,p.waitFor()以便执行该语句的线程等待直到该过程完成。然后,您可以立即执行清理/重新启动逻辑,因为该代码将在进程终止时执行。然而,我不确定如果进程挂起而不是死亡,这将如何工作,但这可能值得一试。顺便说一句,如果您要在生产中执行此操作,我建议您使用Java Service Wrappersupervisord