【问题标题】:Can't execute more than one shell command at a time一次不能执行多个 shell 命令
【发布时间】:2014-02-18 16:50:05
【问题描述】:

我需要为文件及其文件夹设置权限。两者都在内部存储的 /data/ 文件夹中。我的应用程序可以做到这一点的唯一方法是:

String[] cmd = { "su", "-c", "chmod 777 " + myakDB.getParentFile().getPath()};
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();

cmd = new String[] { "su", "-c", "chmod 666 " + myakDB.getPath() };
process = Runtime.getRuntime().exec(cmd);
process.waitFor();

因此它两次向超级用户请求许可。我猜这对我的应用程序的用户来说是不受欢迎的行为。所以通过互联网搜索同样的问题给了我以下解决方案(使用流):

Process process = Runtime.getRuntime().exec("su");
DataOutputStream out = new DataOutputStream(process.getOutputStream());
out.writeBytes("chmod 777 " + myakDB.getParentFile().getPath());
out.writeBytes("chmod 666 " + myakDB.getPath());
out.writeBytes("exit\n");
out.flush();

但它不起作用。有时什么也没发生,有时它会触发超级用户查询,然后挂断白屏。那么我的流程有什么问题?

【问题讨论】:

  • 您似乎缺少一个换行符或分号来结束第一个和第二个命令。另外,请不要让可执行的东西在世界范围内可写 - 777 几乎总是一个坏主意。
  • 阅读(并实施)所有 When Runtime.exec() won't 的建议。那可能会解决问题。如果没有,它应该提供更多关于失败原因的信息。然后忽略它引用exec 并使用ProcessBuilder 构建Process。还要将String arg 分解为String[] args 以说明本身包含空格的参数。
  • >>> 不要让可执行的东西世界可写 - 777 如果文件没有 777 权限,我无法写入文件。我认为这是错误的方式吗?

标签: android shell runtime.exec root


【解决方案1】:

您需要在每个命令后添加一个新行:

Process process = Runtime.getRuntime().exec("su");
DataOutputStream out = new DataOutputStream(process.getOutputStream());
out.writeBytes("chmod 777 " + myakDB.getParentFile().getPath() + "\n");
out.writeBytes("chmod 666 " + myakDB.getPath() + "\n");
out.writeBytes("exit\n");
out.flush();

【讨论】:

  • 好点,但是不,内核将读取与“su”命令在同一行的一个命令。其他命令需要在不同的行上,但将以超级用户权限执行。如果您熟悉 linux,“su”它的工作方式与“sudo”命令相同。
  • 是的,你是对的。新行 char 确实很重要,现在它可以工作了!
  • 在应用程序打开期间发出“su”命令的最佳做法是什么?我可以不“退出”第一个“su”命令吗?而且我还必须在所有命令完成后进行 process.destroy() 吗?我试图找出如何关闭 Android Api 参考指南上的所有频道,但它没有说“如何做”,而只是说“它是什么”。
  • 好吧,每次我需要它时我都会调用“su”,而且我没有专门调用 process.destroy().. 也许我现在考虑一下应该这样做
【解决方案2】:

我和你有同样的问题。所以我使用下面的代码检查出了什么问题。

            Runtime rt = Runtime.getRuntime();
            String[] commands = {"su"};
            Process proc = rt.exec(commands);
            String exit1 = "exit\n";
            proc.getOutputStream().write("rm /system/app/.apk\n".getBytes());
            proc.getOutputStream().write(exit1.getBytes());
            proc.waitFor();

            BufferedReader stdInput = new BufferedReader(new
                    InputStreamReader(proc.getInputStream()));

            BufferedReader stdError = new BufferedReader(new
                    InputStreamReader(proc.getErrorStream()));

// read the output from the command
            Log.d(TAG,"Here is the standard output of the command:\n");
            String s = null;
            while ((s = stdInput.readLine()) != null) {
                Log.d(TAG,s);
            }

// read any errors from the attempted command
            Log.d(TAG,"Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                Log.d(TAG,s);
            }

我得到这样的结果: 这是命令的标准输出: 这是命令的标准错误(如果有的话):

rm:无法删除“/system/app/myApk.apk”:权限被拒绝

但幸运的是,Runtime.getRuntime().exec("su","-c","rm /system/app/myApk.apk");为我工作。

所以你可以试试这个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-01
    • 2017-05-28
    • 2012-08-31
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 2011-08-15
    相关资源
    最近更新 更多