有没有办法ps
在 Java 中定期运行 Unix 命令(在我的情况下)?我写的循环:
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 作业
编辑:结束后我正在制作一个新Thread
的相同类型(PS),所以我肯定每次都在制作一个新的 ProcessBuilder。
编辑:我把没有工作的循环放回去了,因为它引起了混乱。