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:
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)
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:
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 |