<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    tbwshc

    用 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.printStackTrace();  
            }  

    return -1;  
        }  

     

    private
    static String getProcOutput(Process proc) throws IOException, InterruptedException {  
            InputStream is = proc.getInputStream();  
            String line;  
            StringBuffer strResult = new StringBuffer();  


            BufferedReader reader = new BufferedReader(new InputStreamReader(is));  

    while ((line = reader.readLine()) != null) {  
                strResult.append(line);  
            }  
            is.close();  

     

    return strResult.toString().trim();  
        }  

     

    private
    static
    void LOG(String info) {  

    if (info != null && !info.equals("")) {  
                System.out.println("windows服務監控器--------" + getCurrentTime() + "----------->" + info);  
            }  
        }  

     

    private
    static String getCurrentTime() {  
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");  

    return sdf.format(new Date());  
        }  

     

    // public enum ExecuteStates {


    //


    // SUCCEED(0, ""), STATERR_STOPPED(1, "服務已停止"), STATERR_STATED(3, "服務已開始"),


    // STATERR_NOTFOUND(


    // 2, "服務名無效");


    //


    // ExecuteStates(int code, String desc) {


    // this.code = code;


    // this.desc = desc;


    // }


    //


    // private final int code;


    // private final String desc;


    //


    // // regular get method


    // public String getDesc() {


    // return desc;


    // }


    //


    // public static String getDescByCode(int code){


    // for (ExecuteStates e:ExecuteStates.values()){


    // if(e.code==code){


    // return e.desc;


    // }


    // }


    // return null;


    // }


    // }

     


    posted on 2012-07-25 15:30 chen11-1 閱讀(2126) 評論(0)  編輯  收藏

    主站蜘蛛池模板: 亚洲不卡中文字幕| 亚洲国产视频一区| 男女作爱免费网站| 在线观看91精品国产不卡免费| 国产成人精品日本亚洲网址| 足恋玩丝袜脚视频免费网站| 中文字幕不卡免费高清视频| 亚洲综合国产精品第一页| 又粗又长又爽又长黄免费视频| 亚洲片国产一区一级在线观看| 色吊丝性永久免费看码 | 天天摸天天操免费播放小视频| 亚洲人成影院午夜网站| 最新欧洲大片免费在线 | 久久夜色精品国产亚洲| 亚洲精品免费视频| 亚洲w码欧洲s码免费| 午夜免费福利在线| 一级中文字幕乱码免费| 亚洲午夜福利在线观看| 69视频在线是免费观看| 亚洲国产精品免费观看| 免费国产精品视频| a毛片在线免费观看| 亚洲乱码中文字幕小综合| 狠狠久久永久免费观看| 国产成人精品免费视频大全| 亚洲午夜久久久精品影院| 成年在线观看免费人视频草莓| 色婷婷精品免费视频| 亚洲VA中文字幕不卡无码| 99久久综合国产精品免费| 小说专区亚洲春色校园| 亚洲成色WWW久久网站| 久久这里只有精品国产免费10| 人人爽人人爽人人片A免费 | 亚洲国产精品免费观看 | 最近中文字幕无吗免费高清| 免费中文字幕视频| 亚洲精品中文字幕无乱码| 国产在线a不卡免费视频|