It's more efficient to run multiple commands with one XMLSERVICE call than to do many calls

@param array $cmd string will be turned into an array
@param string $exec
@return string
960200
public function buildCommandXmlIn($cmd, $exec = 'pase')
    {
        $xmlIn = '';
        
        // get hex/ccsid information to pass along on the command.
        $propArray = array();
        $this->processParamProps($propArray);
        
        $ccsidHexStr = "{$propArray['ccsidStr']}{$propArray['hexStr']}"; 
        
        // if a string, make a single-item array from it.
        if (is_string($cmd)) {
            $cmdArray = array($cmd);
        } else {
            // already multiple.
            $cmdArray = $cmd;
        }
        
        foreach ($cmdArray as $oneCmd) {
            // Run PASE utility 'system', which runs an IBM i native command.
            // We call this an "interactive" command because it can return output from DSP* interactive commands.
            // Use double quotes around $cmd so that $cmd can safely contain single quotes.
            if ($exec == 'pase') {
                // -i is nice. @todo test speed
                $oneCmd = '/QOpenSys/usr/bin/system "' . $oneCmd . '"';
            }
            
            // if need to convert to hex, do so.
            $encodedCmd = $this->encodeString($oneCmd);
            
            // with pase and pasecmd is <sh, not <cmd.            
            // inner command depends on what was passed in.
            if ($exec == 'pase' || $exec == 'pasecmd') {
                $xmlIn .= "<sh rows='on'$ccsidHexStr>$encodedCmd</sh>";
            } else {
                // Not a "<sh>" tag. Try one of the cmd exec= command styles.
                
                // check that our exec value is in whitelist.
                // blank is a synonym for default of cmd.
                if ($exec && in_array($exec, $this->_cmdTypes)) {
                    $execStr = " exec='$exec'"; // e.g. <cmd exec='rexx'>
                } else {
                     $execStr = ''; // OK because blank string defaults to cmd on XMLSERVICE side
                }
                $xmlIn .= "<cmd$execStr$ccsidHexStr>$encodedCmd</cmd>";
            }
        }
        
        return $this->addOuterTags($xmlIn);
    }
527459
public function bind($cmd, $handler)
    {
        $cmds = array_map('trim', explode(' ', $cmd));

        foreach ($cmds as $cmd) {
            if ($cmd) {
                if (!isset($this->listeners[$cmd])) {
                    $this->listeners[$cmd] = array();
                }

                if ((is_array($handler) && count($handler) == 2 && is_object($handler[0]) && method_exists($handler[0], $handler[1]))
                || function_exists($handler)) {
                    $this->listeners[$cmd][] = $handler;
                }
            }
        }

        return $this;
    }
670006
public function pushOneDoc($fields, $cmd) {
        $cmdName = Constant::get('DOC_KEY_CMD');
        $fieldName = Constant::get('DOC_KEY_FIELDS');
        $this->docs[] = array($cmdName => $cmd, $fieldName => $fields);
    }
155425
protected function exec($cmd, $task, $args = null)
    {
        $script = __DIR__.'/../../exec/'.$cmd.'/'.$task.'.sh';
        if (!file_exists($script)) {
            die('Producer >  INTERNAL ERROR MISSING SCRIPT');
        }

        $params = '';

        if ($args && count($args) > 0) {
            foreach ($args as &$value) {
                $value = $this->escapeParam($value);
            }

            $params = implode(' ', $args);
        }

        $rawCommand = $script.' '
            .$this->escapeParam($this->cwd).' '
            .$this->escapeParam($this->projectsDir).' '.$params;

        $output = shell_exec($rawCommand);

        if (!$this->silent) {
            echo $output;
        }
    }
847512
public function run($cmd, array $args)
    {
        if (!isset($this->commands[$cmd])) {
            $this->error(sprintf('command %s not exists', $cmd));
        }
        $response = new Response();
        $driverId = isset($args['target']) ? $this->getDriverId($args['target']) : '';
        $driverId = !$driverId && isset($args['targets']) ? $this->getDriverId(current($args['targets'])) : $driverId;
        $interface = $this->getInterfaceByCmd($cmd);
        /*
         * @var string          $name
         * @var DriverInterface $driver
         */
        $data = null;
        foreach ($this->driverList as $name => $driver) {
            if (!$driverId || $driverId == $name) {
                if ($driver instanceof $interface) {
                    if ($driver->mount()) {
                        $data = $this->runCmd($driver, $cmd, $args, $response);
                    }
                    $driver->unmount();
                } else {
                    $this->error(sprintf('command "%s" not supported, please use interface "%s"', $cmd, $interface));
                }
                $this->addDisabledCommand($driver);
                $response->setOptions($driver->getOptions());
            }

            $response->addFile($driver->getRootFileInfo());
        }
        if (!empty($args['init'])) {
            $response->setApi(DriverInterface::VERSION);
        }

        return $data ?: $response;
    }
184249
public function bind($cmd, $handler) {
		$allCmds = array_keys($this->commands);
		$cmds = array();
		foreach(explode(' ', $cmd) as $_cmd) {
			if ($_cmd !== '') {
				if ($all = strpos($_cmd, '*') !== false) {
					list(, $sub) = array_pad(explode('.', $_cmd), 2, '');
					if ($sub) {
						$sub = str_replace('\'', '\\\'', $sub);
						$addSub = create_function('$cmd', 'return $cmd . \'.\' . trim(\'' . $sub . '\');');
						$cmds = array_merge($cmds, array_map($addSub, $allCmds));
					} else {
						$cmds = array_merge($cmds, $allCmds);
					}
				} else {
					$cmds[] = $_cmd;
				}
			}
		}
		$cmds = array_unique($cmds);
		
		foreach ($cmds as $cmd) {
			if (!isset($this->listeners[$cmd])) {
				$this->listeners[$cmd] = array();
			}

			if (is_callable($handler)) {
				$this->listeners[$cmd][] = $handler;
			}
		}

		return $this;
	}
1318431
public function exec($cmd): ProcessStatus
    {
        $cmd = sprintf('%s %s', $this->phpBinaryPath, $cmd . ' &');
        if (PHP_OS === 'WINNT') {
            $cmd = str_replace('/', '\\', $cmd);
            $cmd = str_replace('\\', '\\\\', $cmd);
        }
        if (PHP_SAPI !== 'cli') {
//            define('STDIN', fopen('php://stdin', 'r'));
            define('STDOUT', fopen('php://stdout', 'w'));
            define('STDERR', fopen('php://stderr', 'w'));
        }
        $pipes = [['pipe', 'r'], STDOUT, STDERR];
        $pipesOpened = [];
        Log::log($cmd);
        $resource = proc_open($cmd, $pipes, $pipesOpened, null, null, ['bypass_shell' => true]);
        if (false === $resource) {
            throw new \RuntimeException('Cannot be run ' . $cmd);
        }
        return new ProcessStatus($resource, $pipesOpened);
    }
734998
public function cmd($cmd, array $args = array(), $selector = null)
    {
        if (!$this->matched) {
            $this->matched = true;
            return $this;
        }

        $selector = Phery::coalesce($selector, $this->last_selector);

        if ($selector === null) {
            $this->data['0' . ($this->internal_cmd_count++)] = array(
                'c' => $cmd,
                'a' => $args
            );
        } else {
            if (!isset($this->data[$selector])) {
                $this->data[$selector] = array();
            }
            $this->data[$selector][] = array(
                'c' => $cmd,
                'a' => $args
            );
        }

        if ($this->restore !== null) {
            $this->last_selector = $this->restore;
            $this->restore = null;
        }

        return $this;
    }
1027618
public function executeCommandInProcess($cmd, $timeout = null){
        Context::get('output')->write("<comment>$cmd</comment>\n\n");
        $process = new Process($cmd);

        if ($timeout !== null) {
            $process->setTimeout($timeout);
        }

        $process->run(function ($type, $buffer) {
            Context::get('output')->write($buffer);
        });
        return $process;
    }
563797
private function run(string $cmd, string $cwd = null): bool
    {
        $process = new Process($cmd, $cwd);

        $process->setTimeout(300);

        if ($process->isTty()) {
            $process->setTty(true);
        }

        try {
            $output = $this->app->make(OutputInterface::class);
        } catch (Throwable $e) {
            $output = null;
        }

        if ($output) {
            $output->write("\n");
            $process->run(
                function ($type, $line) use ($output) {
                    $output->write($line);
                }
            );
        } else {
            $process->run();
        }

        return $process->isSuccessful();
    }
698724
public static String exec(final String[] cmds, final long timeout) {
        try {
            final Process process = new ProcessBuilder(cmds).redirectErrorStream(true).start();
            final StringWriter writer = new StringWriter();
            new Thread(() -> {
                try {
                    IOUtils.copy(process.getInputStream(), writer, "UTF-8");
                } catch (final Exception e) {
                    LOGGER.log(Level.ERROR, "Reads input stream failed: " + e.getMessage());
                }
            }).start();

            if (!process.waitFor(timeout, TimeUnit.MILLISECONDS)) {
                LOGGER.log(Level.WARN, "Executes commands [" + Arrays.toString(cmds) + "] timeout");
                process.destroy();
            }

            return writer.toString();
        } catch (final Exception e) {
            LOGGER.log(Level.ERROR, "Executes commands [" + Arrays.toString(cmds) + "] failed", e);

            return null;
        }
    }
888773
private function execute($cmd)
    {
        $process = new Process($cmd, $this->sourceFolder);
        $process->run();
        if (!$process->isSuccessful()) {
            throw new \RuntimeException($process->getErrorOutput());
        }
        $this->output = trim($process->getOutput(), PHP_EOL);
    }
1167373
protected function executeCommand($cmd)
    {
        // Shell-escape all arguments except the command.
        $args = func_get_args();
        foreach ($args as $index => $arg) {
            if ($index !== 0) {
                $args[$index] = escapeshellarg($arg);
            }
        }

        // And replace the arguments.
        $command = call_user_func_array('sprintf', $args);
        $output = '';
        if ($this->io->isVerbose()) {
            $this->io->write('<comment>' . $command . '</comment>');
            $io = $this->io;
            $output = function ($type, $data) use ($io) {
                if ($type === Process::ERR) {
                    $io->write('<error>' . $data . '</error>');
                } else {
                    $io->write('<comment>' . $data . '</comment>');
                }
            };
        }
        return ($this->executor->execute($command, $output) === 0);
    }
20284
protected static function execCommand($options, $cmd, array $args, $ifError)
    {
        if (is_array($options) && isset($options['path'])) {
            $cmd = $options['path'];
        } else {
            $executableFinder = new ExecutableFinder;
            $cmd = $executableFinder->find($cmd);
        }

        $out = '';
        $process = ProcessBuilder::create(array_merge(array($cmd), $args))->getProcess();
        if (isset($options['timeout'])) {
            $process->setTimeout($options['timeout']);
        }
        $process->run(function($type, $buffer) use (&$out) { $out .= $buffer; });

        if (!$process->isSuccessful()) {
            throw new \RuntimeException($ifError."\n\n".$out);
        }
    }
995247
public static function make( $cmd, $params )
	{
		if ( !is_array( $params ) )
		{
			$params = array( $params );
		}
		
		$forge = new static;
		
		if ( !method_exists( $forge, $cmd ) )
		{
			throw new CCException( "CCForge_Php - Command could not be found." );
		}
		
		return call_user_func_array( array( $forge, $cmd ), $params );
	}
512078
public function runLocalCommand($cmd, $timeout = 120)
    {
        $this->log($cmd, LogLevel::INFO);

        $process = new Process($cmd);
        $process->setTimeout($timeout);
        $process->run();

        $this->log($process->getOutput(), LogLevel::DEBUG);
        if (!$process->isSuccessful()) {
            $this->log($process->getErrorOutput(), LogLevel::ERROR);
        }

        return $process;
    }
1336097
public function exec($cmd, $write = '', $keepalive = false)
    {
        if ($keepalive) {
            return system($cmd);
        } else {
            $cmd = escapeshellcmd($cmd);
            $process = proc_open($cmd, [
            0 => ['pipe', 'r'],
            1 => ['pipe', 'w'],
            2 => ['pipe', 'w']], $pipes);

            if (!is_resource($process)) {
                return ['Could not open resource stream', 1];
            }

            fwrite($pipes[0], $write);
            fclose($pipes[0]);


            $stdout = stream_get_contents($pipes[1]);
            fclose($pipes[1]);

            $stderr = stream_get_contents($pipes[2]);
            fclose($pipes[2]);

            $rtn = proc_close($process);

            return [$rtn ? $stderr : $stdout, $rtn];
        }
    }