Download the PHP package react/child-process without Composer
On this page you can find all versions of the php package react/child-process. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
All versions of child-process with dependencies
Informations about the package child-process
Event-driven library for executing child processes with ReactPHP.
This library integrates Program Execution
with the EventLoop.
Child processes launched may be signaled and will emit an
exit event upon termination.
Additionally, process I/O streams (i.e. STDIN, STDOUT, STDERR) are exposed
Table of contents
- Quickstart example
- Stream Properties
- Custom pipes
- Sigchild Compatibility
- Windows Compatibility
See also the examples.
Once a process is started, its I/O streams will be constructed as instances of
start() is called, these properties are not set. Once a process terminates,
the streams will become closed but not unset.
Following common Unix conventions, this library will start each child process with the three pipes matching the standard I/O streams as given below by default. You can use the named references for common use cases or access these as an array with all three pipes.
Note that this default configuration may be overridden by explicitly passing
custom pipes, in which case they may not be set or be assigned
different values. In particular, note that Windows support
is limited in that it doesn't support non-blocking STDIO pipes. The
array will always contain references to all pipes as configured and the standard
I/O references will always be set to reference the pipes matching the above
conventions. See custom pipes for more details.
Process class allows you to pass any kind of command line string:
The command line string usually consists of a whitespace-separated list with
your main executable bin and any number of arguments. Special care should be
taken to escape or quote any arguments, escpecially if you pass any user input
along. Likewise, keep in mind that especially on Windows, it is rather common to
have path names containing spaces and other special characters. If you want to
run a binary like this, you will have to ensure this is quoted as a single
escapeshellarg() like this:
By default, PHP will launch processes by wrapping the given command line string
sh command on Unix, so that the first example will actually execute
sh -c echo test under the hood on Unix. On Windows, it will not launch
processes by wrapping them in a shell.
This is a very useful feature because it does not only allow you to pass single
commands, but actually allows you to pass any kind of shell command line and
launch multiple sub-commands using command chains (with
others) and allows you to redirect STDIO streams (with
2>&1 and family).
This can be used to pass complete command lines and receive the resulting STDIO
streams from the wrapping shell command like this:
Note that Windows support is limited in that it doesn't support STDIO streams at all and also that processes will not be run in a wrapping shell by default. If you want to run a shell built-in function such as
sleep 10, you may have to prefix your command line with an explicit shell like
cmd /c echo hello.
In other words, the underlying shell is responsible for managing this command
line and launching the individual sub-commands and connecting their STDIO
streams as appropriate.
This implies that the
Process class will only receive the resulting STDIO
streams from the wrapping shell, which will thus contain the complete
input/output with no way to discern the input/output of single sub-commands.
If you want to discern the output of single sub-commands, you may want to implement some higher-level protocol logic, such as printing an explicit boundary between each sub-command like this:
As an alternative, considering launching one process at a time and listening on
exit event to conditionally start the next process in the chain.
This will give you an opportunity to configure the subsequent process I/O streams:
Keep in mind that PHP uses the shell wrapper for ALL command lines on Unix. While this may seem reasonable for more complex command lines, this actually also applies to running the most simple single command:
This will actually spawn a command hierarchy similar to this on Unix:
This means that trying to get the underlying process PID or sending signals will actually target the wrapping shell, which may not be the desired result in many cases.
If you do not want this wrapping shell process to show up, you can simply
prepend the command string with
exec on Unix platforms, which will cause the
wrapping shell process to be replaced by our process:
This will show a resulting command hierarchy similar to this:
This means that trying to get the underlying process PID and sending signals will now target the actual command as expected.
Note that in this case, the command line will not be run in a wrapping shell.
This implies that when using
exec, there's no way to pass command lines such
as those containing command chains or redirected STDIO streams.
As a rule of thumb, most commands will likely run just fine with the wrapping
If you pass a complete command line (or are unsure), you SHOULD most likely keep
the wrapping shell.
If you're running on Unix and you want to pass an invidual command only, you MAY
want to consider prepending the command string with
exec to avoid the wrapping shell.
exit event will be emitted whenever the process is no longer running.
Event listeners will receive the exit code and termination signal as two
null if the process has terminated, but the exit
code could not be determined (for example
sigchild compatibility was disabled).
null unless the process has terminated in response to
an uncaught signal sent to it.
This is not a limitation of this project, but actual how exit codes and signals
are exposed on POSIX systems, for more details see also
It's also worth noting that process termination depends on all file descriptors
being closed beforehand.
This means that all process pipes will emit a
event before the
exit event and that no more
data events will arrive after
Accordingly, if either of these pipes is in a paused state (
or internally due to a
pipe() call), this detection may not trigger.
terminate(?int $signal = null): bool method can be used to send the
process a signal (SIGTERM by default).
Depending on which signal you send to the process and whether it has a signal
handler registered, this can be used to either merely signal a process or even
forcefully terminate it.
Keep the above section in mind if you want to forcefully terminate a process.
If your process spawn sub-processes or implicitly uses the
wrapping shell mentioned above, its file descriptors may be
inherited to child processes and terminating the main process may not
necessarily terminate the whole process tree.
It is highly suggested that you explicitly
close() all process pipes
accordingly when terminating a process:
For many simple programs these seamingly complicated steps can also be avoided
by prefixing the command line with
exec to avoid the wrapping shell and its
inherited process pipes as mentioned above.
Many command line programs also wait for data on
STDIN and terminate cleanly
when this pipe is closed.
For example, the following can be used to "soft-close" a
While process pipes and termination may seem confusing to newcomers, the above properties actually allow some fine grained control over process termination, such as first trying a soft-close and then applying a force-close after a timeout.
Following common Unix conventions, this library will start each child process with the three pipes matching the standard I/O streams by default. For more advanced use cases it may be useful to pass in custom pipes, such as explicitly passing additional file descriptors (FDs) or overriding default process pipes.
Note that passing custom pipes is considered advanced usage and requires a more in-depth understanding of Unix file descriptors and how they are inherited to child processes and shared in multi-processing applications.
If you do not want to use the default standard I/O pipes, you can explicitly pass an array containing the file descriptor specification to the constructor like this:
Unless your use case has special requirements that demand otherwise, you're
highly recommended to (at least) pass in the standard I/O pipes as given above.
The file descriptor specification accepts arguments in the exact same format
as the underlying
Once the process is started, the
$pipes array will always contain references to
all pipes as configured and the standard I/O references will always be set to
reference the pipes matching common Unix conventions. This library supports any
number of pipes and additional file descriptors, but many common applications
being run as a child process will expect that the parent process properly
assigns these file descriptors.
Internally, this project uses a work-around to improve compatibility when PHP
has been compiled with the
--enable-sigchild option. This should not affect most
installations as this configure option is not used by default and many
distributions (such as Debian and Ubuntu) are known to not use this by default.
Some installations that use Oracle OCI8
may use this configure option to circumvent
When PHP has been compiled with the
--enable-sigchild option, a child process'
exit code cannot be reliably determined via
To work around this, we execute the child process with an additional pipe and
use that to retrieve its exit code.
This work-around incurs some overhead, so we only trigger this when necessary
and when we detect that PHP has been compiled with the
Because PHP does not provide a way to reliably detect this option, we try to
inspect output of PHP's configure options from the
setSigchildEnabled(bool $sigchild): void method can be used to
explicitly enable or disable this behavior like this:
Note that all processes instantiated after this method call will be affected.
If this work-around is disabled on an affected PHP installation, the
event may receive
null instead of the actual exit code as described above.
Similarly, some distributions are known to omit the configure options from
phpinfo(), so automatic detection may fail to enable this work-around in some
cases. You may then enable this explicitly as given above.
Note: The original functionality was taken from Symfony's Process compoment.
Due to platform constraints, this library provides only limited support for
spawning child processes on Windows. In particular, PHP does not allow accessing
standard I/O pipes on Windows without blocking. As such, this project will not
allow constructing a child process with the default process pipes and will
instead throw a
LogicException on Windows by default:
There are a number of alternatives and workarounds as detailed below if you want to run a child process on Windows, each with its own set of pros and cons:
As of PHP 8, you can start the child process with
socketpair descriptors in place of normal standard I/O pipes like this:
socketpairs support non-blocking process I/O on any platform, including Windows. However, not all programs accept stdio sockets.
This package does work on
Windows Subsystem for Linux(or WSL) without issues. When you are in control over how your application is deployed, we recommend installing WSL when you want to run this package on Windows.
If you only care about the exit code of a child process to check if its execution was successful, you can use custom pipes to omit any standard I/O pipes like this:
Similarly, this is also useful if your child process communicates over sockets with remote servers or even your parent process using the Socket component. This is usually considered the best alternative if you have control over how your child process communicates with the parent process.
If you only care about command output after the child process has been executed, you can use custom pipes to configure file handles to be passed to the child process instead of pipes like this:
Note that this example uses
fopen()for illustration purposes only. This should not be used in a truly async program because the filesystem is inherently blocking and each call could potentially take several seconds. See also the Filesystem component as an alternative.
If you want to access command output as it happens in a streaming fashion, you can use redirection to spawn an additional process to forward your standard I/O streams to a socket and use custom pipes to omit any actual standard I/O pipes like this:
Note how this will spawn another fictional
foobarhelper program to consume the standard output from the actual child process. This is in fact similar to the above recommendation of using socket connections in the child process, but in this case does not require modification of the actual child process.
In this example, the fictional
foobarhelper program can be implemented by simply consuming all data from standard input and forwarding it to a socket connection like this:
Accordingly, this example can also be run with plain PHP without having to rely on any external helper program like this:
See also example #23.
Note that this is for illustration purposes only and you may want to implement some proper error checks and/or socket verification in actual production use if you do not want to risk other processes connecting to the server socket. In this case, we suggest looking at the excellent createprocess-windows.
Additionally, note that the command given to the
Process will be
passed to the underlying Windows-API
as-is and the process will not be launched in a wrapping shell by default. In
particular, this means that shell built-in functions such as
echo hello or
sleep 10 may have to be prefixed with an explicit shell command like this:
This will install the latest supported version:
See also the CHANGELOG for details about version upgrades.
This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.3 through current PHP 8+ and HHVM. It's highly recommended to use PHP 7+ for this project.
See above note for limited Windows Compatibility.
To run the test suite, you first need to clone this repo and then install all dependencies through Composer:
To run the test suite, go to the project root and run:
MIT, see LICENSE file.