欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-02-19 13:53:58
...

问题:

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("gcc", "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("gcc", "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命令)并返回执行结果