windows定时重启java程序_windows系统定时重启自定义exe程序

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值