proc_terminate() sends a termination signal to a child process started through proc_open(). This guide explains the function with focused examples and the decisions that matter in real applications.
What Is proc_terminate() in PHP?
Sends a termination signal to a child process started through proc_open().
A typical call looks like this:
<?php
proc_terminate($process, [$signal]);
The input arguments shown in the call identify the relevant connection, resource, value, path or configuration for the operation. Pay attention to argument order and to the return value demonstrated below.
Example 1: Basic Usage
This first example uses proc_terminate() directly to demonstrate its basic purpose.
<?php
$process = proc_open(["php", "-r", "sleep(30);"], [], $pipes); var_dump(proc_terminate($process)); proc_close($process);
The returned value illustrates sends a termination signal to a child process started through proc_open(). Termination is not guaranteed to be immediate; close process resources afterward.
Example 2: A Different Input or Option
The next example changes the input or option, showing another supported use of proc_terminate().
<?php
$process = proc_open(["php", "-r", "sleep(30);"], [], $pipes); var_dump(proc_terminate($process, 15)); proc_close($process);
The returned value illustrates sends a termination signal to a child process started through proc_open(). Termination is not guaranteed to be immediate; close process resources afterward.
Practical Example: Applying the Function
In application code, proc_terminate() belongs in a controlled workflow with input validation, a checked result and cleanup where needed.
<?php
$process = proc_open(["php", "-r", "sleep(30);"], [], $pipes); if (is_resource($process)) { proc_terminate($process); proc_close($process); echo "Stop requested"; }
The returned value illustrates sends a termination signal to a child process started through proc_open(). Termination is not guaranteed to be immediate; close process resources afterward.
Important Behavior and Edge Cases
- Main behavior: Sends a termination signal to a child process started through proc_open().
- Important limitation: Termination is not guaranteed to be immediate; close process resources afterward.
- Requirements: The behavior and privileges of child processes vary across systems.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
proc_terminate() | Sends a termination signal to a child process started through proc_open(). |
proc_close | Closes a process opened with proc_open() and returns its exit status. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the specific limits or preconditions of proc_terminate(). | Termination is not guaranteed to be immediate; close process resources afterward. |
| Assuming every environment produces the same result. | The behavior and privileges of child processes vary across systems. |
Key Takeaways
- Sends a termination signal to a child process started through proc_open().
- Termination is not guaranteed to be immediate; close process resources afterward.
- The behavior and privileges of child processes vary across systems.
Use proc_terminate() when its behavior matches your task, and verify the relevant return value before taking further action. Run examples that affect processes, users, privileges or the filesystem only in a controlled development environment.