Windows system timer restarts custom EXE program

Source: Internet
Author: User

The Windows system periodically restarts the custom EXE program as required by the job. Write the following procedure to modify four parameters in a batch file according to the instructions (Readme.txt):

1.readme.txt

First parameter: Process name (without EXE) second parameter: Large-screen exe path third parameter: Timed task cycle time (seconds) fourth parameter: Interval between end and restart (MS)

2. batch file Dp.bat (Note: The third parameter unit is S, and the fourth parameter is MS)

Java-jar Dp.jar  Foxmail d:\\tools\\foxmail\\foxmail.exe

3.task.java (Export the project as an executable jar package Dp.jar, my operating environment is jdk1.7, but the JDK relationship should be small, not tested ...)

 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 October 1, 2017 pm 1:51:58 *@version1.0*/ Public classTask {StaticString cmdstr1= ""; //end process based on PID     Public Static voidKillprocessbypidname (String pidname)throwsException {runtime.getruntime (). EXEC ("taskkill/f/im" + pidname + ". exe"); }        //start a process based on the Pidname,exe path     Public Static voidStart (string filepath, string pidname)throwsException {runtime.getruntime (). EXEC ("CMD.EXE/C Start" +filepath); }    //Get all Processes     Public StaticList Getcurrosallpidnameset (String pidname)throwsException {Set<String> Pidnameset =NewHashset<>(); List L=NewArraylist<>(); 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 NewException ("Get system All process name error! "); } finally {            if(BR! =NULL) {br.close (); }            if(IR! =NULL) {ir.close (); }            if(Is! =NULL) {is.close (); }        }                returnl; }     Public Static voidMain (string[] args) {FinalScheduledexecutorservice Service =Executors.newsinglethreadscheduledexecutor (); FinalString pidname = args[0];//parameter 1--process name         FinalString cmdStr1 = args[1];//parameter 2--exe path         intTime = Integer.parseint (args[2]);//Parameter 3--timed task cycle interval (seconds)         FinalLong time2 = Long.parselong (args[3]);//Parameter 4--the interval between ending the process and restarting the process directly (in milliseconds)SYSTEM.OUT.PRINTLN ("----Process name:" +pidname); System.out.println ("----exe path:" +cmdStr1); System.out.println ("----timed task cycle interval (milliseconds):" +Time ); System.out.println ("----The interval between ending the process and restarting the process directly (in milliseconds):" +time2); Runnable Runnable=NewRunnable () { Public voidrun () {System.out.println (); System.out.println ("--------------------------"+NewDate () + New task start------------------); Try{System.out.println (NewDate () + "Start End process:" +Getcurrosallpidnameset (pidname));                Thread.Sleep (time2);                Killprocessbypidname (Pidname); System.out.println (NewDate () + "START process started" +Getcurrosallpidnameset (pidname));                Start (Cmdstr1,pidname); System.out.println (NewDate () + "Current process:" +Getcurrosallpidnameset (pidname)); System.out.println ("--------------------------"+NewDate () + "New Task End------------------");            System.out.println (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); Try{killprocessbypidname (pidname);                Service.shutdown (); } Catch(Exception E1) {//TODO auto-generated Catch blockE1.printstacktrace (); } System.out.println ("--------------------------"+NewDate () + "task Exception------------------");            System.out.println ();        }            }        }; Service.scheduleatfixedrate (runnable,0, time, timeunit.seconds); }}

Windows system timer restarts custom EXE program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.