处理windows 下shutdown.bat 杀不死tomcat 进程 2021-10-08 07:34 518阅读 0赞 windows操作系统下 之前通过shutdown.bat 最后发现根本杀不死tomcat相关的进程 try { boolean check=true; Properties props=System.getProperties(); //获得系统属性集 String osName = props.getProperty("os.name"); //操作系统名称 if(osName!=null&&osName.contains("Window")){ check=true; }else{ check=false; } if(check){//windows String location = System.getenv("CATALINA_HOME"); executeCmd(location); }else{//linux String commands="killall java"; Runtime runtime = Runtime.getRuntime(); Process pro = runtime.exec(commands); pro.waitFor(); commands="/**/**/apache-tomcat-7.0.65/bin/startup.sh"; pro = runtime.exec(commands); pro.waitFor(); } } catch(Exception e){ return "fail"; } return "success"; private static void executeCmd(String location) { System.out.println(location); Runtime run = Runtime.getRuntime(); try { Process ps = run.exec("" + location + "\\bin\\shutdown.bat"); BufferedReader br = new BufferedReader(new InputStreamReader( ps.getInputStream(), "utf-8"));// 注意中文编码问题 String line; while ((line = br.readLine()) != null) { System.out.println("StartedLog==>" + line); } br.close(); } catch (Exception e) { e.printStackTrace(); } } 最后发现杀不死tomcat进程 改进后:我直接通过tomcat 的端口号 根据端口号去查找所有与之相关的进程 然后在杀死 private Set<Integer> ports;//在类名下方定义一个变量 try { //关闭quartz任务; Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.shutdown(); Thread.sleep(2000); boolean check=true; Properties props=System.getProperties(); //获得系统属性集 String osName = props.getProperty("os.name"); //操作系统名称 if(osName!=null&&osName.contains("Window")){ check=true; }else{ check=false; } if(check){ //这里是Tomcat端口好 我这边是写死了,这也可以通过代码获取 String port[]={"10012"}; killPort(port); }else{ String commands="killall java"; Runtime runtime = Runtime.getRuntime(); Process pro = runtime.exec(commands); pro.waitFor(); commands="/**/**/apache-tomcat-7.0.65/bin/startup.sh"; pro = runtime.exec(commands); pro.waitFor(); } } catch(Exception e){ return "fail"; } return "success"; public static void killPort(String[] _ports) { Set<Integer> ports = new HashSet<>(); for (String spid : _ports) { try { int pid = Integer.parseInt(spid); ports.add(pid); } catch (Exception e) { System.out.println("错误的端口号,请输入一个或者多个端口,以英文逗号隔开"); try { Thread.sleep(3000); } catch (InterruptedException e1) { e1.printStackTrace(); } System.exit(0); } } //machNetSetDaoImp 表示当前的类名 machNetSetDaoImp kill = new machNetSetDaoImp(); kill.ports = ports; System.out.println("need kill " + ports.size() + " num"); for (Integer pid : ports) { kill.start(pid); } System.out.println("清理完毕,程序即将退出"); System.out.println("SUCCESS"); System.exit(0); } public void kill(List<String> data) { Set<Integer> pids = new HashSet<>(); for (String line : data) { int offset = line.lastIndexOf(" "); String spid = line.substring(offset); spid = spid.replaceAll(" ", ""); int pid = 0; try { pid = Integer.parseInt(spid); } catch (NumberFormatException e) { System.out.println("获取的进程号错误:" + spid); } pids.add(pid); } } public void start(int port){ Runtime runtime = Runtime.getRuntime(); try { //查找进程号 Process p = runtime.exec("cmd /c netstat -ano | findstr \""+port+"\""); InputStream inputStream = p.getInputStream(); List<String> read = read(inputStream, "UTF-8"); if(read.size() == 0){ System.out.println("找不到该端口的进程"); try { Thread.sleep(6000); System.exit(0); } catch (InterruptedException e) { e.printStackTrace(); } }else{ for (String string : read) { System.out.println(string); } System.out.println("找到"+read.size()+"个进程,正在准备清理"); kill(read); } } catch (IOException e) { e.printStackTrace(); } } private List<String> read(InputStream in,String charset) throws IOException{ List<String> data = new ArrayList<>(); BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset)); String line; while((line = reader.readLine()) != null){ boolean validPort = validPort(line); if(validPort){ data.add(line); } } reader.close(); return data; } private boolean validPort(String str){ Pattern pattern = Pattern.compile("^ *[a-zA-Z]+ +\\S+"); Matcher matcher = pattern.matcher(str); matcher.find(); String find = matcher.group(); int spstart = find.lastIndexOf(":"); find = find.substring(spstart + 1); int port = 0; try { port = Integer.parseInt(find); } catch (NumberFormatException e) { System.out.println("查找到错误的端口:" + find); return false; } if(this.ports.contains(port)){ return true; }else{ return false; } }
相关 windows下强制杀死tomcat进程 在Windows操作系统中,我们在启动一个tomcat服务器时,经常会发现8080端口已经被占用的错误,而我们又不知道如何停止这个tomcat服务器。 本文将通过命令来... 朱雀/ 2021年03月10日 04:07/ 0 赞/ 912 阅读
相关 处理windows 下shutdown.bat 杀不死tomcat 进程 windows操作系统下 之前通过shutdown.bat 最后发现根本杀不死tomcat相关的进程 try { boolean check=tru Myth丶恋晨/ 2021年10月08日 07:34/ 0 赞/ 519 阅读
相关 windows 下杀手tomcat 进程 因为我tomcat 端口号为 10012 1.cmd netstat -ano|findstr 10012 ![20181221164838672.jpg][ 红太狼/ 2021年10月08日 07:38/ 0 赞/ 199 阅读
相关 windows下强制杀死tomcat进程 在Windows操作系统中,我们在启动一个tomcat服务器时,经常会发现8080端口已经被占用的错误,而我们又不知道如何停止这个tomcat服务器。 本文将通过命令来强行终 末蓝、/ 2022年05月25日 09:50/ 0 赞/ 355 阅读
相关 TomCat杀进程 有时候当你的tomcat启动时会发现 因为报以下的错误: “Several ports ( 8080, 8009) required by Tomcat v6.0 Serve 谁借莪1个温暖的怀抱¢/ 2022年06月17日 11:26/ 0 赞/ 99 阅读
相关 C\C++杀不死的进程 http://blog.csdn.net/lynch0571/article/details/32965169 通常情况下编写一个程序,可以点击关闭按钮 清疚/ 2022年07月12日 12:07/ 0 赞/ 110 阅读
相关 window杀进程命令 ![1355306463_7775.png][] [1355306463_7775.png]: https://img-my.csdn.net/uploads/201212 客官°小女子只卖身不卖艺/ 2022年08月06日 02:28/ 0 赞/ 102 阅读
相关 windows下强制杀死tomcat进程 在Windows[操作系统][Link 1]中,我们在启动一个tomcat服务器时,经常会发现8080端口已经被占用的错误,而我们又不知道如何停止这个tomcat服务器。 本 短命女/ 2022年09月27日 09:16/ 0 赞/ 184 阅读
相关 linux下tomcat shudown命令杀不死进程 1. 修改tomcat下bin/catalina.sh,记录tomcat的pid 设置记录CATALINA_PID。 该设置会在启动时候bin下 系统管理员/ 2022年11月25日 00:36/ 0 赞/ 60 阅读
相关 Windows下速杀端口占用进程 如杀占用3002的进程: netstat -ano | findstr "3002" taskkill /F /PID 20804 ![在这里插入图片描述] 叁歲伎倆/ 2022年11月27日 07:11/ 0 赞/ 85 阅读
还没有评论,来说两句吧...