我编写了一个java程序,使用fping
协议定期监视设备的状态。它将每5分钟执行fping
命令。我有时只得到一个上述例外。我们正在使用这个目录(/ usr / local / bin /)。这是安装fping
的地方。请建议。
public static List<String> executeProcess(String command,List<String> args,String dirPath) throws Exception {
// delegate
args.add(0,command);
args.add(1,FpingSession.FPING_RETRY);
args.add(2,FpingSession.FPING_RETRY_VALUE);
ProcessBuilder processBuilder = new ProcessBuilder(args);
if(dirPath!=null && !dirPath.isEmpty())
processBuilder.directory(new File(dirPath));
List<String> stringList;
Process process = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
process = processBuilder.start();
inputStream = process.getInputStream();
inputStreamReader = new InputStreamReader(
inputStream);
bufferedReader = new BufferedReader(
inputStreamReader);
stringList = new LinkedList<String>();
while (true) {
// next line
final String string = bufferedReader.readLine();
if (string == null) {
break;
}
// track
stringList.add(string);
}
} finally{
if (process != null)
process.destroy();
if (inputStream != null)
inputStream.close();
if (inputStreamReader != null)
inputStreamReader.close();
if (bufferedReader != null)
bufferedReader.close();
}
// done
return stringList;
}