Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if process is running on windows/linux

Tags:

java

process

i have a 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)) ;

i want to keep a check on this process whether its running or has crashed in case of crash want to restart it, this Process can have multiple instance available depending upon the port

Can i trace this thing on Linux as well as on windows? Read some articles on it but this 1 is bit different, since it involves multiple occurrences and have to check on some particular process only

like image 778
Varun Avatar asked Apr 27 '11 04:04

Varun


People also ask

How do I check if a process is running in Linux?

You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time.

How can I tell if a specific process is running?

Bash commands to check running process: pgrep command – Looks through the currently running bash processes on Linux and lists the process IDs (PID) on screen. pidof command – Find the process ID of a running program on Linux or Unix-like system.

How check if process is running PID Linux?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.

How do you check which process are running in Windows?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column. Click on any column name to sort.


3 Answers

You can do a p.waitFor() so the thread that executed the statement waits till the process is complete. You can then do the cleanup/restart logic right after, as that code will get executed when the process dies. However I am not sure how this would work if the process hangs instead of dying, but this could be worth a try. By the way I would recommend using Java Service Wrapper and supervisord in your case if this is something you're going to do on production.

like image 52
lobster1234 Avatar answered Oct 09 '22 13:10

lobster1234


boolean isRunning(Process process) {
    try {
        process.exitValue();
        return false;
    } catch (Exception e) {
        return true;
    }
}

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

like image 30
AlikElzin-kilaka Avatar answered Oct 09 '22 14:10

AlikElzin-kilaka


As of Java 8 you can do:

process.isAlive()

like image 43
Pace Avatar answered Oct 09 '22 13:10

Pace