Created
March 21, 2022 03:33
-
-
Save flashvnn/81e8c2a9c5fc8cdcff2401c7e25943a4 to your computer and use it in GitHub Desktop.
PHP kill process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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