2011-09-28 44 views
0

我写了一个程序来监视连接到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命令行的一小段时间后做,但它确实。

+0

时间使用调试器。 – bmargulies

+0

请清理您发布的代码的缩进以使其可读。 –

+0

你是否在例外时打印了'd'? –

回答

0

我找到的解决方案是,如果存在一个目录,而不是通过Linux命令行做它使用Java库,用于测试。

例:

File location = new File("directory"); 
if (location.exists()) 
{ 

} 

不知道为什么它的工作原理,并不会崩溃,那里的Linux命令行的一小段时间后做,但它确实。

1

这是没有直接回答你的问题,但它仍然可以帮助你:

我经常要找到像你(很长的方法与“全局”变量代码中的错误,那就是,在声明的变量一个方法的开始,然后全部使用)。只需对代码进行适当的重构(每种方法都有一个简单的方法),错误的原因就会立即显现出来,并在一秒之内得到修复(而重构本身需要更长的时间)。

我想这就是每个人都试图为您提供帮助的是无论如何:重构您的代码(可能只是在头上),以便更容易理解正在发生的事情。

+0

我设法解决了我的问题。不幸的是,stackoverflow不允许我将我的解决方案发布6个小时。只要时间到了,我会发布解决方案,现在我正在编辑我的帖子。谢谢! – Max

+0

+1自己解决 – michael667