您可以使用pgrep找到PID,然后将其杀死:
private void killChromedriver() throws IOException, InterruptedException { String command = "pgrep chromedriver"; Process process = Runtime.getRuntime().exec(command); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); List<String> processIds = getProcessedIds (process, br); for (String pid: processIds) { Process p = Runtime.getRuntime().exec("kill -9 " + pid); p.waitFor(); p.destroy(); } } private List<String> getProcessedIds(Process process, BufferedReader br) throws IOException, InterruptedException { process.waitFor(); List<String> result = new ArrayList<>(); String processId ; while (null != (processId = br.readLine())) { result.add(processId); } process.destroy(); return result; }
更新
另一个更简单的解决方案似乎是
Runtime.getRuntime().exec("pkill chromedriver");
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)