Verify If a Process Is Running Using Its Pid in Java

Verify if a process is running using its PID in JAVA

On posix systems the typical way to query if a pid is running is to send it a null signal e.g. kill(pid, 0). If the call succeeds the process exists; if it returns ESRCH it does not. This is naturally subject to unavoidable race conditions which amount to much less in reality than they do in theory. Less uniform ways are to read the /proc file system (if the OS has one) which is more work, amounts to the same thing, and is still subject to the same race conditions.

Note that pid lockfile technique can be two-tiered. That is, the running process creates the file, locks it, and writes its pid. It holds the lock for the duration of its run and thus this pretty much does away with the above need to query whether the process is running because if the process crashes the file lock will be automatically released even though the file still exists. The logic goes like this:

if file exists
if can get lock
prev instance died unnaturally
continue with this new process
else
instance already running
else
good to go, continue with new process

This technique also has race conditions.

I don't remember enough Java to say whether it has wrappers for kill or file locking syscalls like flock, lockf and fcntl required to implement this scheme.

Java, Checking if any process ID is currently running on Windows

See if this can help:

http://blogs.oracle.com/vaibhav/entry/listing_java_process_from_java

That post explains how to get all PIDs running on a Windows machine: you'd have to compare the output of the cmd call with your PID, instead of printing it.

If you're on Unix-like systems you'd have to use with ps instead of cmd

Calling system commands from your java code is not a very portable solution; then again, the implementation of processes varies among operating systems.

How to check if a process is running on Windows?

There is no direct way to query general processes as each OS handles them differently.

You kinda stuck with using proxies such as direct OS commands...

You can however, find a specific process using tasklist.exe with /fi parameter.

e.g: tasklist.exe /nh /fi "Imagename eq chrome.exe"

Note the mandatory double quotes.

Syntax & usage are available on MS Technet site.

Same example, filtering for "chrome.exe" in Java:

String findProcess = "chrome.exe";
String filenameFilter = "/nh /fi \"Imagename eq "+findProcess+"\"";
String tasksCmd = System.getenv("windir") +"/system32/tasklist.exe "+filenameFilter;

Process p = Runtime.getRuntime().exec(tasksCmd);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

ArrayList<String> procs = new ArrayList<String>();
String line = null;
while ((line = input.readLine()) != null)
procs.add(line);

input.close();

Boolean processFound = procs.stream().filter(row -> row.indexOf(findProcess) > -1).count() > 0;
// Head-up! If no processes were found - we still get:
// "INFO: No tasks are running which match the specified criteria."

Is it possible to create an instance of Process class if I have pid? Java

This isn't what Process is for:

Process provides control of native processes started by ProcessBuilder.start and Runtime.exec.

That's not to say you can't control already-running processes from Java; it's just that Process isn't the thing you should use to do it.

You can make your own class to do what you say you need, e.g:

interface ExternalProcess {
boolean isRunning();

void kill();
}

with implementations of the methods such as:

  • Verify if a process is running using its PID in JAVA
  • Killing a process in Java

Find if process is alive in windows

If all you need is equivalent of ps aux | grep PID, try tasklist /nh /fi "pid eq PID".

How to find the process id of a running Java process on Windows? And how to kill the process alone?

You can use the jps utility that is included in the JDK to find the process id of a Java process. The output will show you the name of the executable JAR file or the name of the main class.

Then use the Windows task manager to terminate the process. If you want to do it on the command line, use

TASKKILL /PID %PID%

Java: Get a process given a pid

I don't think this is possible using only the builtin library. AFAIK, it is already non-trivial to get the running process' own PID (see the feature request and alternate mechanisms).

A quick look at the java.lang.Process class shows that you could go about writing your custom implementation of java.lang.Process using JNI and native code. Your custom class could then implement extra methods, such as the one in your question.

How to find the process id that is running my Java Application

You could use jps to view current Java processes, then pass the output to taskkill. Call this script with the full package.Classname of the process to kill:

rem Run as killpid.cmd package.Classname 
@echo off
jps -l |findstr %1 > %TEMP%\pid.txt

echo FOUND:
type %TEMP%\pid.txt

for /f %%i in (%TEMP%\pid.txt) do taskkill /pid %%i


Related Topics



Leave a reply



Submit