Skip to content

Core\Process\Handle::kill

$handle->kill(): void

Stops a program that Core\Process::spawn started, at once, whatever the program is doing. Use it when you do not need the rest of a program's work: for example, you have read enough of its output, or the program runs too long.

After kill(), call wait() to get the exit code. A program that was stopped does not return 0. On Linux and macOS the exit code is -1.

If the program has already ended, kill() does nothing and throws no error. So you can call it without checking first. Call kill() or wait() before your script or request ends. A program that is still running then is stopped, and an error is reported.

The examples below stop a program that never ends, call kill() on a program that has already ended, and read only the first lines of a long output.

Return value

Returns void. Nothing. Call wait afterwards to get the exit code. It is not 0 for a program that was stopped.

Errors

  • IOErrorThe operating system could not stop the program.

Examples

Stopping a program that never ends

<?nvs
// Shows how to stop a program that would otherwise run forever.
// requires: unix
// `yes` prints the same line again and again, and never stops by itself.
var $endless = Core\Process::spawn('/usr/bin/yes', ['ready']);
// This reads one part of the output, and prints its first line: `ready`.
?tainted bytes $part = $endless->readStdout();
echo Core\Str::lines($part as string)[0], "\n";
// `kill()` stops the program. `wait()` then returns its exit code.
// A stopped program has no exit code of its own, so this prints `-1`.
$endless->kill();
echo $endless->wait()->exitCode(), "\n";
Output
ready
-1

A program that has already ended

<?nvs
// Shows that `kill()` does nothing when the program has already ended.
// requires: unix
var $greeting = Core\Process::spawn('/bin/echo', ['hello']);
// `wait()` waits until `echo` ends. This prints `hello` and then `0`.
var $result = $greeting->wait();
echo $result->stdout() as string;
echo $result->exitCode(), "\n";
// The program has ended, so `kill()` has nothing to stop. It throws no error.
$greeting->kill();
echo "kill() did nothing\n";
Output
hello
0
kill() did nothing

A preview of a long output

<?nvs
// Shows a small method that returns the first lines a program prints, then stops the program.
// requires: unix
class Preview
{
public static function firstLines(string $path, array<string> $arguments, int $count): array<tainted string>
{
var $program = Core\Process::spawn($path, $arguments);
tainted string $text = '';
array<tainted string> $lines = [];
?tainted bytes $part = $program->readStdout();
while ($part != null) {
$text = $text . ($part as string);
$lines = Core\Str::lines($text);
// The last line can be incomplete. So the loop stops at one line more than needed.
if (Core\Arr::count($lines) as int > $count) {
break;
}
$part = $program->readStdout();
}
// The rest of the output is not needed. `kill()` stops the program now.
$program->kill();
$program->wait();
return Core\Arr::slice($lines, 0, $count);
}
}
// `seq 1 1000000` prints one million numbers. This prints only 1, 2 and 3.
foreach (Preview::firstLines('/usr/bin/seq', ['1', '1000000'], 3) as tainted string $line) {
echo $line, "\n";
}
Output
1
2
3