2016-04-14 6 views
1

私はこのプロセスのID(pid)を持っています。
今、私はこのIDのプロセスがいつ起動したのかを知りたいと思います。windows/linuxのプロセスの開始時刻を取得しますか?

注1:プロセスはJavaスレッドではありません。 注2:JNAソリューションもまた歓迎されます。

私のJavaコンテキストからは、この情報を取得したいと思います。
どうすればできますか?

更新:注2を参照してください。

+2

重複 - http://stackoverflow.com/questions/54686/how-to-get-a-list-of-current-op en-windows-process-with-java – deeveeABC

+0

私にとって重要なのは開始時間ですが、このリストからも取得できますか? – Gobliins

+0

Linuxの場合は、 "ps -eo pid、stime"を使用して、PIDの隣に表示されている開始時刻を取得します。 javaからこのコマンドを実行するには、http://stackoverflow.com/questions/54686/how-to-get-a-list-of-current-open-windows-process-with-javaと同様の方法で – mdewit

答えて

1
Linux上

(私は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; 
    } 
} 

出力

水曜日4月13日夜9時13分10秒2016

は、コマンドラインから確認

[email protected]:~$ ps -ewo pid,lstart | grep 29489 
29489 Wed Apr 13 21:13:10 2016 
+0

doあなたは、 "ps"と同じコマンドがウィンドウにあるかどうかを知っていますか? – Gobliins

+0

@ゴブリン私はWindows上でそれを達成する方法を知らない。 –

関連する問題