2

I have the ID (pid) of this process.
Now i want to find out when the process with this id has started.

Note1: The process is not a java thread. Note2: JNA Solutions would also be welcome

From my java context i want to get this information.
How can it be done?

UPDATE: see Note2.

Gobliins
  • 3,848
  • 16
  • 67
  • 122
  • important for me is the starting time, can i get it also from this list? – Gobliins Apr 14 '16 at 11:46
  • For Linux, use "ps -eo pid,stime" to get the start time listed next to the PID. You can run this command from java in a similar manner as in http://stackoverflow.com/questions/54686/how-to-get-a-list-of-current-open-windows-process-with-java – mdewit Apr 14 '16 at 12:06

1 Answers1

2

On linux (I am running Ubuntu 14)

public class SO {

    public static void main(String[] args) throws Exception {
        System.out.println(getStartTime(29489));
    }   

    private static String getStartTime(int pid) throws IOException {

        String start = null;        
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("ps -ewo pid,lstart");
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = "";       
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            if (line.startsWith(pid+" ")) {
                int firstSpace = line.indexOf(" "); 
                start = line.substring(firstSpace+1);
                break;
            }
        }       
        return start;
    }
}

Output

Wed Apr 13 21:13:10 2016

Verifying through command line

xxx@xxx:~$ ps -ewo pid,lstart | grep 29489
29489 Wed Apr 13 21:13:10 2016
uncaught_exception
  • 1,068
  • 6
  • 15