Topic: java 調用 LINUX service ntpd stop

  Print this page

1.java 調用 LINUX service ntpd stop Copy to clipboard
Posted by: skwujinhua
Posted on: 2008-11-14 10:48

我要用java停掉linux 的ntpd 服務﹐代碼如下﹕
import java.io.*;
import java.util.*;
import java.lang.*;
public class Stop_ntpd
{
public static void main(String args[])
{
try{
String comm = "service ntpd stop";
Process proc = Runtime.getRuntime().exec(comm);
}catch (IOException e)
{
System.err.println(e.getMessage());
}
}
}

編譯執行都正確﹐但是我用service ntpd status 查看該服務的時候﹐該服務還在運行﹐為什么啊?Process 構造方法我看了N次了Runtime構造也看了N次了。GG JJ們﹐幫我下啊。謝謝。

2.Re:java 調用 LINUX service ntpd stop [Re: skwujinhua] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-11-14 18:42

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.
Jiafan

3.Re:java 調用 LINUX service ntpd stop [Re: skwujinhua] Copy to clipboard
Posted by: skwujinhua
Posted on: 2008-11-17 08:08

Jiafan:
謝謝你的回復。

4.Re:java 調用 LINUX service ntpd stop [Re: skwujinhua] Copy to clipboard
Posted by: skwujinhua
Posted on: 2008-11-17 09:10

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

}
}
剛才才解決﹐執行正確。謝謝Jaifan 提示。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923