packagecom.kd;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.util.ArrayList;importjava.util.Date;importjava.util.HashSet;importjava.util.List;importjava.util.Set;importjava.util.concurrent.Executors;importjava.util.concurrent.ScheduledExecutorService;importjava.util.concurrent.TimeUnit;/***@authorliangyadong
* @date 2017年10月1日 下午1:51:58
*@version1.0*/
public classtask {static String cmdStr1="";//根据pid 结束进程
public static void killProcessByPidName(String pidName) throwsException {
Runtime.getRuntime().exec("taskkill /F /IM " + pidName + ".exe");
}//根据pidname,exe路径 启动进程
public static void start(String filepath, String pidName) throwsException {
Runtime.getRuntime().exec("cmd.exe /c start "+filepath);
}//获取所有进程
public static List getCurrOsAllPidNameSet(String pidname) throwsException {
Set pidNameSet = new HashSet<>();
List l= new ArrayList<>();
InputStream is= null;
InputStreamReader ir= null;
BufferedReader br= null;
String line= null;
String[] array= (String[]) null;try{
Process p= Runtime.getRuntime().exec("TASKLIST /NH /FO CSV");
is=p.getInputStream();
ir= newInputStreamReader(is);
br= newBufferedReader(ir);while ((line = br.readLine()) != null) {
array= line.split(",");
line= array[0].replaceAll("\"", "");
line= line.replaceAll(".exe", "");
line= line.replaceAll(".exe".toUpperCase(), "");if(line.startsWith(pidname)){
l.add(line);
}
}
}catch(IOException localIOException) {throw new Exception("获取系统所有进程名出错!");
}finally{if (br != null) {
br.close();
}if (ir != null) {
ir.close();
}if (is != null) {
is.close();
}
}returnl;
}public static voidmain(String[] args) {final ScheduledExecutorService service =Executors.newSingleThreadScheduledExecutor();final String pidname = args[0];//参数1 -- 进程名称
final String cmdStr1 = args[1];//参数2 -- exe 路径
int time = Integer.parseInt(args[2]);//参数3 -- 定时任务循环间隔(秒)
final Long time2 = Long.parseLong(args[3]);//参数4 -- 结束进程与重新启动进程直接的间隔(毫秒)
System.out.println("----进程名称:"+pidname);
System.out.println("----exe 路径:"+cmdStr1);
System.out.println("----定时任务循环间隔(毫秒):"+time);
System.out.println("----结束进程与重新启动进程直接的间隔(毫秒):"+time2);
Runnable runnable= newRunnable() {public voidrun() {
System.out.println();
System.out.println("--------------------------"+new Date()+"新任务开始------------------");try{
System.out.println(new Date()+"开始结束进程:"+getCurrOsAllPidNameSet(pidname));
Thread.sleep(time2);
killProcessByPidName(pidname);
System.out.println(new Date()+"开始启动进程"+getCurrOsAllPidNameSet(pidname));
start(cmdStr1,pidname);
System.out.println(new Date()+"当前进程:"+getCurrOsAllPidNameSet(pidname));
System.out.println("--------------------------"+new Date()+"新任务结束------------------");
System.out.println();
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();try{
killProcessByPidName(pidname);
service.shutdown();
}catch(Exception e1) {//TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("--------------------------"+new Date()+"任务异常------------------");
System.out.println();
}
}
};
service.scheduleAtFixedRate(runnable,0, time, TimeUnit.SECONDS);
}
}