处理windows 下shutdown.bat 杀不死tomcat 进程

版权声明:创业:https://shop120085695.taobao.com https://blog.csdn.net/zxf1242652895/article/details/84313963

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;
	        }
	    }

猜你喜欢

转载自blog.csdn.net/zxf1242652895/article/details/84313963