基于centos的java编写根据进程名称关闭进程方法
package com.cwgis.closeprocess;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
public class Main {
public static void main(String[] args)
throws Exception
{
String name = ManagementFactory.getRuntimeMXBean().getName();
// get current_pid
String current_pid = name.split("@")[0];
System.out.println(name+ " 当前进程PID="+current_pid);
//args=new String[1];
//args[0]="dd";
// write your code here
//System.out.println("helloworld");
if(args==null) throw new IOException("args参数不能为空");
if(args.length<0)throw new IOException("args参数至少为一个参数");
String pName=args[0];
System.out.println("当前正在查找的进程名称为:"+pName);
boolean havePID = closeProcessBypName(pName,current_pid);
if(havePID==false)
{
System.out.println(" PID未找到进程编号");
}
}
public static boolean closeProcessBypName(String pName,String current_pid){
boolean rbc=false;
BufferedReader reader =null;
try{
String PID="";
String t_pName="";
//显示所有进程
Process process = Runtime.getRuntime().exec("jps");
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while((line = reader.readLine())!=null){
String[] strs = line.split(" ");
if(strs.length>=2) {
PID = strs[0];
t_pName=strs[1];
}
if(PID.equalsIgnoreCase(current_pid)==false && t_pName.equalsIgnoreCase(pName)){
rbc=true;
System.out.println("相关信息 -----> "+line+" 找到PID="+PID+" ");
//
closeLinuxProcess(PID); //kill -9 PID
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
}
}
}
return rbc;
}
/**
* 关闭Linux进程
* @param Pid 进程的PID
*/
public static void closeLinuxProcess(String Pid){
Process process = null;
BufferedReader reader =null;
try{
//杀掉进程
process = Runtime.getRuntime().exec("kill -9 "+Pid);
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while((line = reader.readLine())!=null){
System.out.println("杀掉进程PID="+line);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
}
}
}
}
}
应用例子:
[root@node111 ~]# java -cp /usr/cwgis/tools/closeprocess.jar com.cwgis.closeprocess.Main Main
6429@node111 当前进程PID=6429
当前正在查找的进程名称为:Main
相关信息 -----> 32417 Main 找到PID=32417
相关信息 -----> 31778 Main 找到PID=31778
相关信息 -----> 32326 Main 找到PID=32326
相关信息 -----> 32204 Main 找到PID=32204
runCmd.sh "java -cp /usr/cwgis/tools/closeprocess.jar com.cwgis.closeprocess.Main HRegionServer" all
runCmd.sh "java -cp /usr/cwgis/tools/closeprocess.jar com.cwgis.closeprocess.Main HMaster" all
[root@node111 ~]# runCmd.sh "java -cp /usr/cwgis/tools/closeprocess.jar com.cwgis.closeprocess.Main HRegionServer" all
*******************node111***************************
7317@node111 当前进程PID=7317
当前正在查找的进程名称为:HRegionServer
PID未找到进程编号
*******************node112***************************
32583@node112 当前进程PID=32583
当前正在查找的进程名称为:HRegionServer
PID未找到进程编号
*******************node113***************************
3806@node113 当前进程PID=3806
当前正在查找的进程名称为:HRegionServer
PID未找到进程编号
*******************node114***************************
1710@node114 当前进程PID=1710
当前正在查找的进程名称为:HRegionServer
PID未找到进程编号
*******************node115***************************
20986@node115 当前进程PID=20986
当前正在查找的进程名称为:HRegionServer
PID未找到进程编号
*******************node116***************************
17629@node116 当前进程PID=17629
当前正在查找的进程名称为:HRegionServer
PID未找到进程编号
*******************node117***************************
24213@node117 当前进程PID=24213
当前正在查找的进程名称为:HRegionServer
PID未找到进程编号
*******************node118***************************
23505@node118 当前进程PID=23505
当前正在查找的进程名称为:HRegionServer
PID未找到进程编号
*******************node119***************************
29860@node119 当前进程PID=29860
当前正在查找的进程名称为:HRegionServer
PID未找到进程编号
*******************node120***************************
27533@node120 当前进程PID=27533
当前正在查找的进程名称为:HRegionServer
PID未找到进程编号
*******************node121***************************
18447@node121 当前进程PID=18447
当前正在查找的进程名称为:HRegionServer
PID未找到进程编号
*******************node122***************************
5914@node122 当前进程PID=5914
当前正在查找的进程名称为:HRegionServer
PID未找到进程编号
*******************node123***************************
29428@node123 当前进程PID=29428
当前正在查找的进程名称为:HRegionServer
PID未找到进程编号