当前位置:编程学习 > JAVA >>

用 java实现定时重启windows指定服务

package com.test.processManagement; 

 

import java.io.BufferedReader; 

import java.io.FileNotFoundException; 

import java.io.FileOutputStream; 

import java.io.IOException; 

import java.io.InputStream; 

import java.io.InputStreamReader; 

import java.io.PrintStream; 

import java.text.ParseException; 

import java.text.SimpleDateFormat; 

import java.util.Date; 

import java.util.Timer; 

import java.util.TimerTask; 

 

/**
* <title>ServRebootScheWin</title>
*  
* <project>Exam</project>
*  
* <package>com.test.processManagement</package>
*  
* <file>ServRebootScheWin.java</file>
*  
* <date>2012-7-11</date>
*  
* <brief>本程序用于每天定时重启windows系统上的指定服务,并记录日志</brief>
*  
* @author Wero
*  
*/


public
class ServRebootScheWin { 

 

public
static
void main(String[] args) { 

// store the console output


final PrintStream console = System.out; 

if (args.length < 2) {  
            LOG("参数不全,程序将退出...");  
            Runtime.getRuntime().exit(-1);  
        } 

 

final String timeStr = args[0];// 每tb天重启时间(HH:mm:ss)


final String servName = args[1];// 服务名


if (args.length >= 3) { 

try {  
                System.setOut(new PrintStream(new FileOutputStream(args[2])));  
            } catch (FileNotFoundException e) {  
                System.setOut(console);  
                LOG("日志文件无法建立...");  
            }  
        } 

 

// convert time string to Date type

        Date date = null; 

try {  
            date = new SimpleDateFormat("HH:mm:ss").parse(timeStr);  
        } catch (ParseException e1) {  
            LOG("日期格式(HH:mm:ss)错误,程序将退出...");  
            Runtime.getRuntime().exit(-1);  
        } 

 

// schedule the specific windows service to reboot at specific time


// every day

        rebootEveryDayTime(date, servName); 

 

// add shutdown hook to recover system.out to console when program exits

        Runtime.getRuntime().addShutdownHook(new Thread() { 

@Override


public
void run() {  
                System.setOut(console);  
            }  
        });  
    } 

 

private
static
void rebootEveryDayTime(Date date, final String servName) { 

new Timer().schedule(new TimerTask() { 

public
void run() { 

try {  
                    reboot(servName);  
                } catch (Exception e) {  
                    LOG("重启出现异常:" + e.getMessage());  
                }  
            }  
        }, date, 24 * 60 * 60 * 1000);  
    } 

 

private
static
void reboot(String servName) throws IOException, InterruptedException {  
        LOG("重启服务:" + servName);  
        Process procStop;  
        Process procStart; 

int stopState = -1; 

int startState = -1; 

 

// stop the specific service

        procStop = Runtime.getRuntime().exec("net stop \"" + servName + "\""); 


        stopState = getProcExecStat(procStop);  
        LOG(getProcOutput(procStop)); 

 

// wait for 10 seconds


try {  
            Thread.sleep(10 * 1000);  
        } catch (InterruptedException e) {  
            LOG("线程等待时中断...");  
            e.printStackTrace();  
        } 

 

// restart

        procStart=Runtime.getRuntime().exec("net start \"" + servName + "\"");  
        startState = getProcExecStat(procStart);  
        LOG(getProcOutput(procStart)); 

 

//if stop exec and start exec both return with failed flag,exists


if (stopState != 0 && startState != 0) {  
            LOG("重启失败,请确认服务名是否有效,程序将退出...");  
        } else {  
            LOG("重启成功.");  
        }  
    } 

 

private
static
int getProcExecStat(Process proc) { 

try { 

return proc.waitFor();  
        } catch (InterruptedException e) {  
            LOG("线程等待时中断...");  
            e.printSta

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,