我写了一个程序来监视连接到Linux上的RAID某些硬盘驱动器的状态。通过这个程序,我执行几个命令行命令。一个有趣的错误发生虽然....程序运行好三分钟,似乎它不能再正确执行它以前执行的命令(对于许多迭代)。进程生成递增错误
因为它似乎莫名其妙地错过了驱动器(即使它发现它轻车熟路),它吐出了一个数组索引错误(我的变量driveLetters [d])。
其他注意事项......如果我告诉它重新诠释“d”为“0”,如果超过驱动器的数量...程序将不会崩溃,反而会刚刚成为陷入无限循环。 此外,程序崩溃的时间也各不相同。它在一定数量的间隔后似乎不会崩溃。最后,我没有得到任何类型的内存泄漏错误。
下面是一些代码,应揭露错误:
public static void scsi_generic() throws IOException, InterruptedException
{
int i =0;
int d =0;
int numberOfDrives = 8;
char driveLetters[] = {'b','c','d','e','f','g','h','i','j','k','l','m'};
String drive = "";
while (i <= numberOfDrives)
{
System.out.println("position 1");
List<String> commands = new ArrayList<String>();
commands.add("cat");
commands.add("/sys/class/scsi_generic/sg"+i+"/device/sas_address");
SystemCommandExecutor commandExecutor = new SystemCommandExecutor(commands);
int driveFound = commandExecutor.executeCommand();
if (driveFound == 0)
{
System.out.println("Folder: sg" + i + " was found.");
StringBuilder stdout = commandExecutor.getStandardOutputFromCommand();
String data = stdout.toString();
String sas = data.substring(11,12);
int sasA = Integer.parseInt(sas,16);
boolean matchedSG = false;
while (matchedSG == false)
{
System.out.println("position2");
List<String> lookSD = new ArrayList<String>();
lookSD.add("test");
lookSD.add("-d");
lookSD.add("/sys/class/scsi_generic/sg"+i+"/device/block:sd" + driveLetters[d]);
SystemCommandExecutor commandSearch = new SystemCommandExecutor(lookSD);
int sdFound = commandSearch.executeCommand();
StringBuilder stdout3 = commandSearch.getStandardOutputFromCommand();
StringBuilder stderr = commandSearch.getStandardErrorFromCommand();
String sdFound2 = stdout3.toString();
if (sdFound == 0)
{
matchedSG = true;
System.out.println("Found the SD drive.");
drive = "sd"+driveLetters[d];
System.out.println(sasA);
hdsas.set(sasA , sas);
d = 0;
i++;
loadDrives(drive , sasA);
}
/* else if (sdFound !=)
{
System.out.println("Error:" + sdFound);
System.out.println(d+ " "+ i);
}
*/
else if (d >= 8)
{
System.out.println("Drive letter: " + driveLetters[d]);
System.out.println("Int: " + i);
// System.out.println(sdFound2);
System.out.println("sd error: "+ sdFound);
// System.out.println(stderr);
//System.out.println(sdFound2 + " m");
}
else
{
d++;
}
}
}
else
{
System.out.println("Folder: sg" + i + " could not be found.");
i++;
}
d =0;
}
}
任何帮助或建议将是真棒!谢谢。
编辑:
我找到的解决方案是,如果存在一个目录,而不是通过Linux命令行做它使用Java库,用于测试。
例:
File location = new File("directory");
if (location.exists())
{
}
不知道为什么它的工作原理,并不会崩溃,那里的Linux命令行的一小段时间后做,但它确实。
时间使用调试器。 – bmargulies
请清理您发布的代码的缩进以使其可读。 –
你是否在例外时打印了'd'? –