Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

linux - Verify if a process is running using its PID in JAVA

I'm currently building an application in JAVA where there can be only one execution. So I'm currently using a lock file in which I write the PID of the current execution.

So whenever this application will start, it will open the file (if it exists) and try to detect if the PID written in the file is actually running.

This prevent the problem where my app crash before unlocking the file.

I need this to work both on windows (XP,7 or 8) and linux (all the users are on debian based distros).

Here's some code to give you a better picture of what I want to do :

//get the PID from the file
int pidValue = new FileReader(file).read();

//get the OS type
String os = System.getProperty("os.name").toLowerCase();

//Check PID depending of OS type
if( os.contains("nux") || os.contains("nix") ){
/*
 * Check PID on Linux/Unix
*/
} else if ( os.contains("win") ) {
/*
 * Check PID on Windows
 */
}

I have tried to find documentation on the subject, but I didn't manage to find anything useful yet.

Thank you very much.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following code determines if a process with the specified pid is running. It was tested on Windows 7 and Ubuntu 13. On Windows it uses apache commons-exec to run tasklist and determines if they found the specified pid based on their exit code. It overcomes the fact that tasklist always returns 0 by piping the result to findstr. On linux it uses ps to do the same thing. It also suppresses stdout logging of the child process.

public static boolean isProcessRunning(int pid, int timeout, TimeUnit timeunit) throws java.io.IOException {
    String line;
    if (OS.isFamilyWindows()) {
        //tasklist exit code is always 0. Parse output
        //findstr exit code 0 if found pid, 1 if it doesn't
        line = "cmd /c "tasklist /FI "PID eq " + pid + "" | findstr " + pid + """;
    }
    else {
        //ps exit code 0 if process exists, 1 if it doesn't
        line = "ps -p " + pid;
            //`-p` is POSIX/BSD-compliant, `--pid` isn't<ref>https://github.com/apache/storm/pull/296#discussion_r20535744</ref>
    }
    CommandLine cmdLine = CommandLine.parse(line);
    DefaultExecutor executor = new DefaultExecutor();
    // disable logging of stdout/strderr
    executor.setStreamHandler(new PumpStreamHandler(null, null, null));
    // disable exception for valid exit values
    executor.setExitValues(new int[]{0, 1});
    // set timer for zombie process
    ExecuteWatchdog timeoutWatchdog = new ExecuteWatchdog(timeunit.toMillis(timeout));
    executor.setWatchdog(timeoutWatchdog);
    int exitValue = executor.execute(cmdLine);
    // 0 is the default exit code which means the process exists
    return exitValue == 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...