1. 程式人生 > >windows系統定時重啟自定義exe程序

windows系統定時重啟自定義exe程序

util 文件 shu n) nbsp pat jdk getc exe程序

工作需要, 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); } }

windows系統定時重啟自定義exe程序