【问题标题】:How to get error message of a failed shell command on android如何在android上获取失败的shell命令的错误消息
【发布时间】:2012-02-02 17:11:22
【问题描述】:

在一个root的android设备上,我尝试运行一个读取内核日志的cat命令,如下:

Process p = Runtime.getRuntime().exec("su");
p = Runtime.getRuntime().exec("/system/bin/cat /proc/kmsg");

su 命令执行成功,但 cat 没有执行成功。 我尝试使用 getInputStream() 读取命令的输出,但没有任何内容,如下所示:

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
 while ((read = err.read(buffer)) > 0)
 {  //read error to buffer 
catOutput.append(buffer, 0, read);
 }
 in.close();

我在 ls 命令中使用了相同的代码,而不是显示内核日志,它工作得很好并显示了结果。

我想知道在执行 cat 命令时我遇到了什么错误并希望在 shell 上看到错误消息。尝试了 p.getErrorStream() 但它没有给我任何结果。 有人可以帮我吗?谢谢。

【问题讨论】:

    标签: android shell runtime message


    【解决方案1】:

    这是一个关于如何执行此操作的综合示例 - 请注意,我从 this 答案中得到了这个想法:

    public void catKmsg() {
      Runtime runtime = Runtime.getRuntime();
      Process proc = null;
      OutputStreamWriter osw = null;
      StringBuilder sbstdOut = new StringBuilder();
      StringBuilder sbstdErr = new StringBuilder();
    
      String command="/system/bin/cat /proc/kmsg";
    
      try { // Run Script
    
        proc = runtime.exec("su");
        osw = new OutputStreamWriter(proc.getOutputStream());
        osw.write(command);
        osw.flush();
        osw.close();
    
      } catch (IOException ex) {
        ex.printStackTrace();
      } finally {
        if (osw != null) {
          try {
            osw.close();
          } catch (IOException e) {
            e.printStackTrace();                    
          }
        }
      }
      try {
        if (proc != null) {
          proc.waitFor();
        }
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      sbstdOut.append(ReadBufferedReader(new InputStreamReader
                                         (proc.getInputStream())));
      sbstdErr.append(ReadBufferedReader(new InputStreamReader
                                         (proc.getErrorStream())));
      if (proc.exitValue() != 0) {
      }
    }
    

    【讨论】:

    • 在读取内核日志(即 cat 命令)之前,我必须更改为 root。我该怎么做?
    • 它在我的 android 命令行上不起作用。只有 su 执行,而不是命令的第二部分。这是我得到的:shell@android:/ $ su -c /system/bin/cat
    • @Callitaday 我已经更新了我的答案 - 你可以试试吗?
    • 我试过你的代码,但它挂在 sbstdOut.append(ReadBufferedReader(new InputStreamReader (proc.getInputStream())));
    • @Callitaday 最后一部分可能会挂起,但这个想法仍然成立。使用osw OutputStream 在su 之后 出额外的命令 - 就像您在终端中所做的那样。你将不得不在这里付出一些努力:) 我不能为你做所有的工作。
    【解决方案2】:

    我终于通过使用 RootTools 库找到了问题的解决方案。 最近发布(在我提出问题几个月后),RootTools 提供了一个易于使用的工具集,可帮助运行需要 root 权限的命令。在执行 shell 命令之前,我创建了一个包装器来检查 root 访问是否可用:

    void testRootToolsCommand(String command){
    
        if (RootTools.isRootAvailable())
            toastMessage("Root is available !!!");
        else { 
            toastMessage("NO ROOT !!! ");
            return; 
        }
    
        int timeOut = 1000; 
        try {
            List<String> output = RootTools.sendShell(command,timeOut);     
            toastMessage("OUTPUT of the command \n" + output.toString());           
        } catch (RootToolsException re) {
            toastMessage("Funny thing happened with RootTools!!! ");            
        } catch (TimeoutException te)
        {
            toastMessage("Timeout exception - Increase timeout !!! !!! ");
        }
        catch (Exception e) { 
        toastMessage(e.getMessage().toString());
        }       
    }
    

    函数调用的一个例子是:

    testRootToolsCommand("cat /proc/kmsg > /sdcard/jun11_4h51.txt");
    

    注意:该工具还支持一次运行多个命令。

    【讨论】:

    • 已弃用,请查看最新版本的文档 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 2012-12-28
    相关资源
    最近更新 更多