private void runNextCommand(boolean notifyIdle) {
      // must always be called from a synchronized method

      boolean running = isRunning();
      if (!running)
        idle = true;

      if (running && idle && (commands.size() > 0)) {
        Command command = commands.get(0);
        commands.remove(0);

        buffer = null;
        lastExitCode = 0;
        lastMarkerStdout = null;
        lastMarkerStderr = null;

        if (command.commands.length > 0) {
          try {
            if (command.onCommandResultListener != null) {
              // no reason to store the output if we don't have an OnCommandResultListener.
              // user should catch the output with an OnLineListener in this case.
              buffer = Collections.synchronizedList(new ArrayList<String>());
            }

            idle = false;
            this.command = command;
            startWatchdog();
            for (String write : command.commands) {
              stdin.write((write + "\n").getBytes("UTF-8"));
            }
            stdin.write(("echo " + command.marker + " $?\n").getBytes("UTF-8"));
            stdin.write(("echo " + command.marker + " >&2\n").getBytes("UTF-8"));
            stdin.flush();
          } catch (IOException e) {
            // stdin might have closed
          }
        } else {
          runNextCommand(false);
        }
      } else if (!running) {
        // our shell died for unknown reasons - abort all submissions
        while (commands.size() > 0) {
          postCallback(commands.remove(0), ShellExitCode.SHELL_DIED, null);
        }
      }

      if (idle && notifyIdle) {
        synchronized (idleSync) {
          idleSync.notifyAll();
        }
      }
    }
Run the next command if any and if ready

@param notifyIdle
    signals idle state if no commands left ?
imitate/src/main/java/com/engineer/imitate/shell/Shell.java