Current Location: Home> Function Categories> proc_terminate

proc_terminate

Kill a process opened by proc_open
Name:proc_terminate
Category:Program execution
Programming Language:php
One-line Description:Terminate a process created by the proc_open() function

Function name: proc_terminate()

Applicable version: PHP 4 >= 4.3.0, PHP 5, PHP 7

Function description: The proc_terminate() function is used to terminate a process created by the proc_open() function.

Syntax: bool proc_terminate(resource $process, int $signal = 15)

parameter:

  • $process: The process resource returned by the proc_open() function.
  • $signal (optional): The signal to be sent to the process, defaults to SIGTERM (15).

Return value: Return true if the process is successfully terminated; otherwise return false.

Example:

 $descriptorspec = array( 0 => array("pipe", "r"), // 标准输入,子进程从此管道中读取数据1 => array("pipe", "w"), // 标准输出,子进程向此管道中写入数据2 => array("file", "/tmp/error-output.txt", "a") // 标准错误,写入到一个文件); $process = proc_open('php -r "echo \'Hello, World!\';"', $descriptorspec, $pipes); // 等待一段时间后终止进程sleep(3); proc_terminate($process); // 读取子进程的输出echo stream_get_contents($pipes[1]); // 关闭管道和进程资源fclose($pipes[0]); fclose($pipes[1]); proc_close($process);

In the above example, we use the proc_open() function to create a child process that executes a simple PHP command to output "Hello, World!". We then terminate the child process after waiting for 3 seconds using the proc_terminate() function. Finally, we read the output of the child process through the stream_get_contents() function and close the relevant pipelines and process resources.

Note that the proc_terminate() function just sends a signal to the process, but it cannot guarantee that the process will be terminated immediately. If you need to ensure that the process terminates immediately, you can use the proc_close() function instead of the proc_terminate() function.

Similar Functions
Popular Articles