2012-03-23 173 views
2
tail -f test.log 

上面的命令將尾部,其運行的幕後過程日誌。我怎樣才能殺死那個特定的進程?殺死Linux進程剛跑

我可以在shell上按「Ctrl-Z」,但是我正在使用Java運行該命令,並且需要終止該進程。

對此的任何幫助將不勝感激。

感謝

回答

3

當你start a subprocess with Java回傳對應於正在運行的進程a Process object 。您可以使用destroy() method on the Process object來終止運行命令。

所以,你會啓動它:

Process p = Runtime.getRuntime().exec(new String[] {"tail","-f","test.log"}); 

,並殺死它:

p.destroy(); 

如果你只是讀出新的行添加到文件,你有沒有考慮本身在做這個Java的?讀取該文件是非常簡單的:

try { 
    BufferedReader input = new BufferedReader(new FileReader("tail.log")); 

    while (true) { 
    String line; 

    while ((line = input.readLine()) != null) { 
     //You'll probably want to do something other than println() 
     System.out.println(line); 
    } 

    try { 
     Thread.sleep(500); 
    } catch (InterruptedException e) { 
     break; 
    } 

    } 

    input.close(); 
} 
catch (IOException ioe) { 
    //Handle this 
} 

你可能會想在另一個Thread運行此。與tail -f不同,上述代碼不處理從頭開始重寫的文件,而不是附加到,但您可以修復該問題。

0

好了,你可以做

ps -ef | grep java 

找到所有的java進程,然後殺了你想要的。