windows系統定時重啟自訂exe程式

來源:互聯網
上載者:User

標籤:直接   acea   擷取   catch   exception   ade   jdk   迴圈   with   

工作需要, 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 5000pause

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程式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.