【发布时间】:2010-11-05 15:04:59
【问题描述】:
有没有办法在 Java 中定期运行 Unix 命令(在我的例子中是 ps)?我写的循环:
while( this.check )
{
try
{
ProcessBuilder pb = new ProcessBuilder("ps");
Process proc;
System.out.println(" * * Running `ps` * * ");
byte[] buffer;
String input;
proc = pb.start();
BufferedInputStream osInput =
new BufferedInputStream(proc.getInputStream());
//prints 0 every time after the first
System.out.println(osInput.available());
buffer = new byte[osInput.available()];
osInput.read(buffer);
input = new String(buffer);
for( String line : input.split("\n"))
{
if( line.equals("") )
continue;
this.handlePS(line);
}
proc.destroy();
try
{
Thread.sleep(10000);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
不起作用。它第一次运行得很好,但此后每次输入流都有 0 个字节可用。我会尝试watch 命令,但这个 Solaris 盒子没有。我不能使用 cron 作业,因为我需要知道 PID 是否存在于 Java 应用程序中。有什么想法吗?
提前致谢。
编辑:无法使用 cron 作业
编辑:我正在制作一个新的相同类型 (PS) 的 Thread,所以我肯定每次都在制作一个新的 ProcessBuilder。
编辑:我把没有工作的循环放回去了,因为它引起了混乱。
【问题讨论】:
-
放回循环以避免任何混乱
标签: java unix solaris inputstream