工作需要, Windows系统定时重启自定义exe程序. 写了如下程序, 按照说明(readme.txt)修改批处理文件中的四个参数即可:
1.readme.txt
第一个参数:进程名(不用带exe)
第二个参数:大屏exe路径
第三个参数:定时任务循环时间(秒)
第四个参数:结束与重启的间隔时间(毫秒)
2.批处理文件dp.bat(注意:第三个参数单位是s,第四个参数单位是ms)
java -jar dp.jar Foxmail D:\Tools\Foxmail\Foxmail.exe 10 5000
pause
3.task.java(将工程导出为可执行jar包dp.jar, 我的运行环境为jdk1.7,不过和jdk关系应该不大,没测试...)
package com.kd; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /** * @author liangyadong * @date 2017年10月1日 下午1:51:58 * @version 1.0 */ public class task { static String cmdStr1=""; //根据pid 结束进程 public static void killProcessByPidName(String pidName) throws Exception { Runtime.getRuntime().exec("taskkill /F /IM " + pidName + ".exe"); } //根据pidname,exe路径 启动进程 public static void start(String filepath, String pidName) throws Exception { Runtime.getRuntime().exec("cmd.exe /c start "+filepath); } //获取所有进程 public static List getCurrOsAllPidNameSet(String pidname) throws Exception { Set<String> 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 = new InputStreamReader(is); br = new BufferedReader(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(); } } return l; } public static void main(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 = new Runnable() { public void run() { 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); } }