Skip to content

Instantly share code, notes, and snippets.

@flashvnn
Created March 21, 2022 03:33
Show Gist options
  • Save flashvnn/81e8c2a9c5fc8cdcff2401c7e25943a4 to your computer and use it in GitHub Desktop.
Save flashvnn/81e8c2a9c5fc8cdcff2401c7e25943a4 to your computer and use it in GitHub Desktop.
PHP kill process
<?php
$exe_command = 'ping google.com';
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout -> we use this
2 => array("pipe", "w") // stderr
);
$process = proc_open($exe_command, $descriptorspec, $pipes);
if (is_resource($process))
{
$count = 0;
while( ! feof($pipes[1]))
{
$return_message = fgets($pipes[1], 1024);
if (strlen($return_message) == 0) break;
echo $return_message;
$count++;
if ($count == 3) {
$s = proc_get_status($process);
posix_kill($s['pid'], SIGKILL);
proc_close($process);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment