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

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

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

    敬的世界

    常用鏈接

    統(tǒng)計

    最新評論

    java 調(diào)用 LINUX service ntpd stop

    If you read about the Javadoc of the Process class, you will discover something
    very weird and unexpected. The following is the context of the Javadoc for Process.
    In effect, you should be very clear of this limitation before you think of using
    the Process JDK. Ok, here it is:


    The methods that create processes may not work well for special processes on certain
    native platforms, such as native windowing processes, daemon processes, Win16/DOS
    processes on Microsoft Windows, or shell scripts.

    The created subprocess does not have its own terminal or console. All its standard
    io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process
    through three streams (getOutputStream(), getInputStream(), getErrorStream()).
    ...


    So basically, Process does not guarantee your daemon process to work properly. This
    is horrifying when I first read about this Javadoc. However, I have changed my
    mind after using Process for a while. My experience tells me that usually Process
    cannot be working at the following circumstances: (assuming we are in Linux O.S.)

    - to execute shell built-in commands e.g. "cd" or "alias"
    - to redirect output from the executed application,
    - This method will NOT expand filesystem wildcards e.g. "*".

    Taking all these things into consideration, let's see what we can do to improve
    your code.

    First of all, Process has its standard outputstream, and its standard errorstream
    (but don't try to redirect them, as it will *not* work)


    Process proc = Runtime.getRuntime().exec(command);

    // Create thread for reading inputStream (process' stdout)
    proc.getInputStream();

    // Create thread for reading errorStream (process' stderr)
    proc.getErrorStream()


    Another fact I can see why your ntpd has not stopped is maybe you havn't given
    enough time for the Process to be executed completely.

    Consider adding the following code into your program:


    // this will wait for the process to end!!! I think it is very important
    // not to interfere with other process in the O.S.
    int returnCode = proc.waitFor();

    String msg = "Shell command [" + commandString + "] result [";
    if (returnCode == 0)
    {
    msg += "SUCCEEDED";
    }
    else
    {
    msg = msg + "FAILED - ret code: " + returnCode;
    }
    msg += "]";



    Then you can print the msg to the standard output or simply trace it into your log.
    You shall see the error if occurs.

    Another thing I can suggest you to do is fully read the Process Javadoc and any related documents about using Process.

    Hope these help.


    import java.io.*;
    public chass shell{
    public static void main(String [] args)throws Exception{
    try

    {

    process = Runtime.getRuntime().exec ("bin/sh service ntpd stop");

    InputStreamReader ir=newInputStreamReader(process.getInputStream());

    LineNumberReader input = new LineNumberReader (ir);


    String line;

    while ((line = input.readLine ()) != null)

    System.out.println(line);

    }

    catch (java.io.IOException e){

    System.err.println ("IOException " + e.getMessage());

    }
    }
    ?

    來自 :http://www.cjsdn.net/post/view?bid=1&id=196586

    posted on 2009-04-26 02:38 picture talk 閱讀(421) 評論(0)  編輯  收藏 所屬分類: Java

    主站蜘蛛池模板: 暖暖免费中文在线日本| 亚洲人AV永久一区二区三区久久| 亚洲人成影院在线无码观看| 亚洲欧美国产精品专区久久| 成人午夜性A级毛片免费| 亚洲中文字幕久久无码| 永久黄网站色视频免费| 亚洲AV无码成人精品区狼人影院| 日韩精品免费一区二区三区| 国产成人精品亚洲日本在线| 亚洲一区在线视频观看| 国产好大好硬好爽免费不卡| 久久精品亚洲综合一品| 久久久国产精品福利免费| 亚洲av午夜福利精品一区| 久久久精品2019免费观看| 亚洲国产精品xo在线观看| 成年人免费视频观看| 国产成人亚洲精品91专区高清 | 免费视频爱爱太爽了| 亚洲色无码专区一区| 免费国产人做人视频在线观看| 一区二区三区免费看| 亚洲国产精品国自产拍电影| 日本在线高清免费爱做网站| 亚洲av无码一区二区三区四区| 亚洲人成人网站在线观看| 成人性生交大片免费看好| 国产精品偷伦视频免费观看了 | 亚洲av永久无码精品古装片| 精品无码免费专区毛片| 亚洲精品女同中文字幕| 国产成人A亚洲精V品无码| 亚欧色视频在线观看免费| 妇女自拍偷自拍亚洲精品| 亚洲精品无码鲁网中文电影| 成年人网站免费视频| 九九免费久久这里有精品23| 麻豆亚洲AV永久无码精品久久| 日韩人妻无码免费视频一区二区三区 | 亚洲Av无码专区国产乱码DVD|