Core\Process\Handle::wait
$handle->wait(): Core\Process\ResultWaits until a program that Core\Process::spawn started has ended, and returns a Core\Process\Result. The result has the exit code, the output and the error output of the program.
wait() first closes the standard input of the program. So a program that reads until its input ends can finish. If you already read part of the output with readStdout() or readStderr(), the result has only the part that you did not read. While wait() waits, the server keeps handling other requests.
If the output is larger than [limits] max_output, wait() stops the program and throws a RuntimeError. You can call wait() a second time. It returns the same exit code and empty output.
The examples below read an exit code and an error message, show the output that was already read, and run three programs at the same time.
Return value
Returns Core\Process\Result. A Core\Process\Result with the exit code, and the output and error output that readStdout and readStderr did not read yet. If you already read all of the output, both are empty. A second call returns the same exit code and empty output.
Errors
RuntimeErrorThe output that was not read yet is larger than[limits] max_output. The program is then stopped.TimeoutErrorThetimeoutgiven tospawnhas passed. The program is stopped, and every later call exceptkillthrows this error too.IOErrorThe operating system could not read the output or get the exit code.
Examples
Reading the exit code
<?nvs// Shows how to wait until a program ends, and then read its exit code and its error output.// requires: unix
// This program writes a message to its error output and ends with the exit code `3`.var $check = Core\Process::spawn('/bin/sh', ['-c', "echo 'disk is full' >&2; exit 3"]);
// `wait()` waits until the program ends. It returns a `Core\Process\Result`.var $result = $check->wait();
// This prints `exit code: 3` and then `disk is full`.echo 'exit code: ', $result->exitCode(), "\n";echo $result->stderr() as string;exit code: 3disk is fullOutput that was already read
<?nvs// Shows that `wait()` returns only the output that `readStdout()` did not read yet.// requires: unix
// This program prints one line, waits one second, and prints a second line.var $program = Core\Process::spawn('/bin/sh', ['-c', 'echo first; sleep 1; echo second']);
// `readStdout()` reads the first line. This prints `read: first`.?tainted bytes $part = $program->readStdout();echo 'read: ', $part as string;
// The result of `wait()` has only the second line. This prints `wait: second`.echo 'wait: ', $program->wait()->stdout() as string;read: firstwait: secondRunning three programs at once
<?nvs// Shows how to start several programs at once, and then wait for each one.// requires: unix
// Three jobs. Each one takes one second, and then prints a line.array<string> $jobs = [ 'sleep 1; echo report ready', 'sleep 1; echo backup done', 'sleep 1; echo cache cleared',];
// `spawn()` does not wait. All three programs start now and run at the same time.array<Core\Process\Handle> $running = [];foreach ($jobs as string $job) { $running[] = Core\Process::spawn('/bin/sh', ['-c', $job]);}
// `wait()` returns the result of each program. The whole script takes about one second, not three.// This prints `0: report ready`, `0: backup done` and `0: cache cleared`.foreach ($running as Core\Process\Handle $program) { var $result = $program->wait(); echo $result->exitCode(), ': ', $result->stdout() as string;}0: report ready0: backup done0: cache cleared