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

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

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

    敬的世界

    常用鏈接

    統計

    最新評論

    java 調用 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 閱讀(425) 評論(0)  編輯  收藏 所屬分類: Java

    主站蜘蛛池模板: 亚洲成_人网站图片| 日日噜噜噜噜夜夜爽亚洲精品| 亚洲AV日韩精品久久久久久久| 亚洲黄片手机免费观看| 亚洲AV无码成H人在线观看| 色天使亚洲综合一区二区| 成人午夜性A级毛片免费| 亚洲精品国产av成拍色拍| 永久免费无码网站在线观看| 亚洲第一综合天堂另类专 | 中文字幕在线观看免费视频| 亚洲AV无码成人专区片在线观看| A级毛片高清免费视频在线播放| 日本亚洲欧洲免费天堂午夜看片女人员 | 国产午夜无码片免费| 国产亚洲av片在线观看16女人| 久久久久免费精品国产小说| 亚洲综合区图片小说区| 国内免费高清在线观看| 蜜桃传媒一区二区亚洲AV| 亚洲精品乱码久久久久久蜜桃| a级毛片在线免费观看| 亚洲天堂视频在线观看| 欧美好看的免费电影在线观看| 亚洲av成人一区二区三区观看在线 | 日本免费v片一二三区| eeuss影院www天堂免费| 久久国产亚洲电影天堂| 成年免费大片黄在线观看岛国 | 国产精品国产午夜免费福利看 | 无码人妻精品一二三区免费| 免费人成大片在线观看播放电影| 丝袜熟女国偷自产中文字幕亚洲| 精品国产麻豆免费人成网站| 亚洲一级视频在线观看| 亚洲人成无码www久久久| 亚洲免费中文字幕| 免费人成再在线观看网站 | 亚洲一卡二卡三卡| 亚洲天堂免费在线视频| 国产免费的野战视频|