[Java]调试笔记 - 问题报错Cannot run program “sh“: error=24, Too many open files

问题:

Android采用RootCmd工具类执行adb命令,执行多次之后报错,导致adb命令无法正常执行,先上报错日志;

2020-11-04 21:10:30.307 5843-5843/com.orange.openclose W/System.err: java.io.IOException: Cannot run program "sh": error=24, Too many open files
2020-11-04 21:10:30.308 5843-5843/com.orange.openclose W/System.err:     at java.lang.ProcessBuilder.start(ProcessBuilder.java:983)
2020-11-04 21:10:30.309 5843-5843/com.orange.openclose W/System.err:     at java.lang.Runtime.exec(Runtime.java:691)
2020-11-04 21:10:30.309 5843-5843/com.orange.openclose W/System.err:     at java.lang.Runtime.exec(Runtime.java:524)
2020-11-04 21:10:30.310 5843-5843/com.orange.openclose W/System.err:     at java.lang.Runtime.exec(Runtime.java:421)
2020-11-04 21:10:30.311 5843-5843/com.orange.openclose W/System.err:     at com.orange.openclose.MainActivity.execRootCommand(MainActivity.java:51)
2020-11-04 21:10:30.311 5843-5843/com.orange.openclose W/System.err:     at com.orange.openclose.MainActivity.onCreate(MainActivity.java:24)
2020-11-04 21:10:30.312 5843-5843/com.orange.openclose W/System.err:     at android.app.Activity.performCreate(Activity.java:6709)
2020-11-04 21:10:30.312 5843-5843/com.orange.openclose W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
2020-11-04 21:10:30.313 5843-5843/com.orange.openclose W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2624)
2020-11-04 21:10:30.313 5843-5843/com.orange.openclose W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732)
2020-11-04 21:10:30.314 5843-5843/com.orange.openclose W/System.err:     at android.app.ActivityThread.-wrap12(ActivityThread.java)
2020-11-04 21:10:30.315 5843-5843/com.orange.openclose W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
2020-11-04 21:10:30.315 5843-5843/com.orange.openclose W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
2020-11-04 21:10:30.316 5843-5843/com.orange.openclose W/System.err:     at android.os.Looper.loop(Looper.java:154)
2020-11-04 21:10:30.316 5843-5843/com.orange.openclose W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6141)
2020-11-04 21:10:30.317 5843-5843/com.orange.openclose W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2020-11-04 21:10:30.318 5843-5843/com.orange.openclose W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
2020-11-04 21:10:30.318 5843-5843/com.orange.openclose W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
2020-11-04 21:10:30.320 5843-5843/com.orange.openclose W/System.err: Caused by: java.io.IOException: error=24, Too many open files
2020-11-04 21:10:30.321 5843-5843/com.orange.openclose W/System.err:     at java.lang.UNIXProcess.forkAndExec(Native Method)
2020-11-04 21:10:30.321 5843-5843/com.orange.openclose W/System.err:     at java.lang.UNIXProcess.<init>(UNIXProcess.java:133)
2020-11-04 21:10:30.322 5843-5843/com.orange.openclose W/System.err:     at java.lang.ProcessImpl.start(ProcessImpl.java:128)
2020-11-04 21:10:30.322 5843-5843/com.orange.openclose W/System.err:     at java.lang.ProcessBuilder.start(ProcessBuilder.java:964)
2020-11-04 21:10:30.322 5843-5843/com.orange.openclose W/System.err: 	... 17 more
2020-11-04 21:10:32.330 5843-5843/com.orange.openclose W/System.err: java.io.IOException: Cannot run program "sh": error=24, Too many open files
2020-11-04 21:10:32.331 5843-5843/com.orange.openclose W/System.err:     at java.lang.ProcessBuilder.start(ProcessBuilder.java:983)
2020-11-04 21:10:32.332 5843-5843/com.orange.openclose W/System.err:     at java.lang.Runtime.exec(Runtime.java:691)
2020-11-04 21:10:32.332 5843-5843/com.orange.openclose W/System.err:     at java.lang.Runtime.exec(Runtime.java:524)
2020-11-04 21:10:32.333 5843-5843/com.orange.openclose W/System.err:     at java.lang.Runtime.exec(Runtime.java:421)
2020-11-04 21:10:32.333 5843-5843/com.orange.openclose W/System.err:     at com.orange.openclose.MainActivity.execRootCommand(MainActivity.java:51)
2020-11-04 21:10:32.334 5843-5843/com.orange.openclose W/System.err:     at com.orange.openclose.MainActivity.onCreate(MainActivity.java:35)
2020-11-04 21:10:32.334 5843-5843/com.orange.openclose W/System.err:     at android.app.Activity.performCreate(Activity.java:6709)
2020-11-04 21:10:32.335 5843-5843/com.orange.openclose W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
2020-11-04 21:10:32.336 5843-5843/com.orange.openclose W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2624)
2020-11-04 21:10:32.336 5843-5843/com.orange.openclose W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732)
2020-11-04 21:10:32.337 5843-5843/com.orange.openclose W/System.err:     at android.app.ActivityThread.-wrap12(ActivityThread.java)
2020-11-04 21:10:32.338 5843-5843/com.orange.openclose W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
2020-11-04 21:10:32.338 5843-5843/com.orange.openclose W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
2020-11-04 21:10:32.339 5843-5843/com.orange.openclose W/System.err:     at android.os.Looper.loop(Looper.java:154)
2020-11-04 21:10:32.339 5843-5843/com.orange.openclose W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6141)
2020-11-04 21:10:32.340 5843-5843/com.orange.openclose W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2020-11-04 21:10:32.341 5843-5843/com.orange.openclose W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
2020-11-04 21:10:32.341 5843-5843/com.orange.openclose W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
2020-11-04 21:10:32.343 5843-5843/com.orange.openclose W/System.err: Caused by: java.io.IOException: error=24, Too many open files
2020-11-04 21:10:32.344 5843-5843/com.orange.openclose W/System.err:     at java.lang.UNIXProcess.forkAndExec(Native Method)
2020-11-04 21:10:32.344 5843-5843/com.orange.openclose W/System.err:     at java.lang.UNIXProcess.<init>(UNIXProcess.java:133)
2020-11-04 21:10:32.345 5843-5843/com.orange.openclose W/System.err:     at java.lang.ProcessImpl.start(ProcessImpl.java:128)
2020-11-04 21:10:32.345 5843-5843/com.orange.openclose W/System.err:     at java.lang.ProcessBuilder.start(ProcessBuilder.java:964)
2020-11-04 21:10:32.345 5843-5843/com.orange.openclose W/System.err: 	... 17 more

源码方法:

private Process execRootCommand(String command) throws IOException {
    if (command == null) {
        Log.w("command is null");
        return null;
    }
    Runtime runtime = Runtime.getRuntime();
    //Process proc = runtime.exec("su");
    Process proc = runtime.exec("sh");
    OutputStream os = proc.getOutputStream();
    String cmd = command + "\n";
    Log.d("cmd  == " + cmd);
    os.write(cmd.getBytes());
    os.flush();
    return proc;
}

百度查阅了下,进程一直开启没有释放,百度查考process.destroy()或者waitfor()可以解决;

直接用了大神的工具类,写了个demo进行循环测试,问题解决。

private  static int execRootCommand(String cmd) {
    int result = -1;
    DataOutputStream dos = null;

    try {
        Process p = Runtime.getRuntime().exec("sh");
        dos = new DataOutputStream(p.getOutputStream());

        //Log.i(TAG, cmd);
        dos.writeBytes(cmd + "\n");
        dos.flush();
        dos.writeBytes("exit\n");
        dos.flush();
        p.waitFor();
        result = p.exitValue();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dos != null) {
            try {
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

DEMO源码:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new updateScreenTimeThread().start();
    }
}
class updateScreenTimeThread extends Thread{
    public static final String TAG = "TestLog";
    public static  int i = 1;
    private  static int execRootCommand(String cmd) {
        int result = -1;
        DataOutputStream dos = null;

        try {
            Process p = Runtime.getRuntime().exec("sh");
            dos = new DataOutputStream(p.getOutputStream());

            //Log.i(TAG, cmd);
            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            p.waitFor();
            result = p.exitValue();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    @Override
    public void run() {
        while(true){
            Log.e(TAG,"iiiiiiiiiii = " + i++);
            execRootCommand("echo 1 > /sys/devices/platform/backlight/backlight/backlight/bl_power");
            Log.e(TAG,"closesrceen");
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            execRootCommand("echo 0 > /sys/devices/platform/backlight/backlight/backlight/bl_power");
            Log.e(TAG,"opensrceen");
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

调试方法:

网友文档:android代码执行adb shell终端命令(linux命令)并返回执行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值