`

windows和linux下使用命令查找端口对应的进程pid并杀死进程

阅读更多
windows下命令,8080是要杀死进程的端口,这个命令自己查找端口对应的进程pid,然后kill
for /f "tokens=5" %a in ('netstat -ao^|findstr 8080') do @taskkill /F /PID %a
public static final void windowsRestart(String port, String path, String param)
    throws IOException, DaemonException, InterruptedException
  {
    String cmd = "for /f \"tokens=5\" %a in ('netstat -ao^|findstr " + port + "') do @taskkill /F /PID %a";
    String[] command = { "cmd", "-c", "start", cmd };
    LOG.info(cmd);
    Process pro = Runtime.getRuntime().exec(command);
    pro.waitFor();
    LOG.info("exitValue=" + pro.exitValue());
    Thread.sleep(5000L);
    cmd = "cmd /c start " + path;
    LOG.info(cmd);
    pro = Runtime.getRuntime().exec(cmd);
    pro.waitFor();
    LOG.info("exitValue=" + pro.exitValue());
  }

  public static final void linuxRestart(String port, String path, String param)
    throws IOException, InterruptedException
  {
    String cmd = "kill -9 $(netstat -tlnp|grep " + port + "|awk '{print $7}'|awk -F '/' '{print $1}')";
    String[] command = { "sh", "-c", cmd };
    LOG.info(cmd);
    Process pro = Runtime.getRuntime().exec(command);
    pro.waitFor();
    LOG.info("exitValue=" + pro.exitValue());
    cmd = path;
    LOG.info(cmd);
    pro = Runtime.getRuntime().exec(cmd);
    pro.waitFor();
    LOG.info("exitValue=" + pro.exitValue());
  }

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics