class Process implements IteratorAggregate
Process is a thin wrapper around proc_* functions to easily start independent PHP processes.
ERR | |
OUT | |
STATUS_READY | |
STATUS_STARTED | |
STATUS_TERMINATED | |
STDIN | |
STDOUT | |
STDERR | |
TIMEOUT_PRECISION | |
ITER_NON_BLOCKING | |
ITER_KEEP_OUTPUT | |
ITER_SKIP_OUT | |
ITER_SKIP_ERR |
static | $exitCodes | Exit codes translation table. |
__construct(string|array $commandline, string|null $cwd = null, array $env = null, mixed|null $input = null, int|float|null $timeout = 60, array $options = null) | ||
__destruct() | ||
__clone() | ||
int | run($callback = null) Runs the process. | |
Process | mustRun(callable $callback = null) Runs the process. | |
start(callable $callback = null) Starts the process and returns after writing the input to STDIN. | ||
$this | restart(callable $callback = null) Restarts the process. | |
int | wait(callable $callback = null) Waits for the process to terminate. | |
int|null | getPid() Returns the Pid (process identifier), if applicable. | |
$this | signal(int $signal) Sends a POSIX signal to the process. | |
$this | disableOutput() Disables fetching output and error output from the underlying process. | |
$this | enableOutput() Enables fetching output and error output from the underlying process. | |
bool | isOutputDisabled() Returns true in case the output is disabled, false otherwise. | |
string | getOutput() Returns the current output of the process (STDOUT). | |
string | getIncrementalOutput() Returns the output incrementally. | |
Generator | getIterator(int $flags) Returns an iterator to the output of the process, with the output type as keys (Process::OUT/ERR). | |
$this | clearOutput() Clears the process output. | |
string | getErrorOutput() Returns the current error output of the process (STDERR). | |
string | getIncrementalErrorOutput() Returns the errorOutput incrementally. | |
$this | clearErrorOutput() Clears the process output. | |
null|int | getExitCode() Returns the exit code returned by the process. | |
null|string | getExitCodeText() Returns a string representation for the exit code returned by the process. | |
bool | isSuccessful() Checks if the process ended successfully. | |
bool | hasBeenSignaled() Returns true if the child process has been terminated by an uncaught signal. | |
int | getTermSignal() Returns the number of the signal that caused the child process to terminate its execution. | |
bool | hasBeenStopped() Returns true if the child process has been stopped by a signal. | |
int | getStopSignal() Returns the number of the signal that caused the child process to stop its execution. | |
bool | isRunning() Checks if the process is currently running. | |
bool | isStarted() Checks if the process has been started with no regard to the current state. | |
bool | isTerminated() Checks if the process is terminated. | |
string | getStatus() Gets the process status. | |
int | stop(int|float $timeout = 10, int $signal = null) Stops the process. | |
addOutput(string $line) Adds a line to the STDOUT stream. | ||
addErrorOutput(string $line) Adds a line to the STDERR stream. | ||
string | getCommandLine() Gets the command line to be executed. | |
Process | setCommandLine(string|array $commandline) Sets the command line to be executed. | |
float|null | getTimeout() Gets the process timeout (max. runtime). | |
float|null | getIdleTimeout() Gets the process idle timeout (max. time since last output). | |
Process | setTimeout(int|float|null $timeout) Sets the process timeout (max. runtime). | |
Process | setIdleTimeout(int|float|null $timeout) Sets the process idle timeout (max. time since last output). | |
Process | setTty(bool $tty) Enables or disables the TTY mode. | |
bool | isTty() Checks if the TTY mode is enabled. | |
Process | setPty(bool $bool) Sets PTY mode. | |
bool | isPty() Returns PTY state. | |
string|null | getWorkingDirectory() Gets the working directory. | |
Process | setWorkingDirectory(string $cwd) Sets the current working directory. | |
array | getEnv() Gets the environment variables. | |
Process | setEnv(array $env) Sets the environment variables. | |
resource|string|Iterator|null | getInput() Gets the Process input. | |
Process | setInput(resource|scalar|Traversable|null $input) Sets the input. | |
array | getOptions() deprecated Gets the options for proc_open. | |
Process | setOptions(array $options) deprecated Sets the options for proc_open. | |
bool | getEnhanceWindowsCompatibility() deprecated Gets whether or not Windows compatibility is enabled. | |
Process | setEnhanceWindowsCompatibility(bool $enhance) deprecated Sets whether or not Windows compatibility is enabled. | |
bool | getEnhanceSigchildCompatibility() deprecated Returns whether sigchild compatibility mode is activated or not. | |
Process | setEnhanceSigchildCompatibility(bool $enhance) deprecated Activates sigchild compatibility mode. | |
Process | inheritEnvironmentVariables(bool $inheritEnv = true) Sets whether environment variables will be inherited or not. | |
bool | areEnvironmentVariablesInherited() deprecated Returns whether environment variables will be inherited or not. | |
checkTimeout() Performs a check between the timeout definition and the time the process started. | ||
static bool | isPtySupported() Returns whether PTY is supported on the current operating system. |
string|array | $commandline | The command line to run |
string|null | $cwd | The working directory or null to use the working dir of the current PHP process |
array | $env | The environment variables or null to use the same environment as the current PHP process |
mixed|null | $input | The input as stream resource, scalar or \Traversable, or null for no input |
int|float|null | $timeout | The timeout in seconds or null to disable |
array | $options | An array of options for proc_open |
RuntimeException | When proc_open is not installed |
Runs the process.
The callback receives the type of output (out or err) and some bytes from the output in real-time. It allows to have feedback from the independent process during execution.
The STDOUT and STDERR are also available after the process is finished via the getOutput() and getErrorOutput() methods.
$callback |
int | The exit status code |
RuntimeException | When process can't be launched |
RuntimeException | When process stopped after receiving signal |
LogicException | In case a callback is provided and output has been disabled |
Runs the process.
This is identical to run() except that an exception is thrown if the process exits with a non-zero exit code.
callable | $callback |
Process |
RuntimeException | if PHP was compiled with --enable-sigchild and the enhanced sigchild compatibility mode is not enabled |
ProcessFailedException | if the process didn't terminate successfully |
Starts the process and returns after writing the input to STDIN.
This method blocks until all STDIN data is sent to the process then it returns while the process runs in the background.
The termination of the process can be awaited with wait().
The callback receives the type of output (out or err) and some bytes from the output in real-time while writing the standard input to the process. It allows to have feedback from the independent process during execution.
callable | $callback |
RuntimeException | When process can't be launched |
RuntimeException | When process is already running |
LogicException | In case a callback is provided and output has been disabled |
Restarts the process.
Be warned that the process is cloned before being started.
callable | $callback |
$this |
RuntimeException | When process can't be launched |
RuntimeException | When process is already running |
start() |
Waits for the process to terminate.
The callback receives the type of output (out or err) and some bytes from the output in real-time while writing the standard input to the process. It allows to have feedback from the independent process during execution.
callable | $callback | A valid PHP callback |
int | The exitcode of the process |
RuntimeException | When process timed out |
RuntimeException | When process stopped after receiving signal |
LogicException | When process is not yet started |
Returns the Pid (process identifier), if applicable.
int|null | The process id if running, null otherwise |
Sends a POSIX signal to the process.
int | $signal | A valid POSIX signal (see http://www.php.net/manual/en/pcntl.constants.php) |
$this |
LogicException | In case the process is not running |
RuntimeException | In case --enable-sigchild is activated and the process can't be killed |
RuntimeException | In case of failure |
Disables fetching output and error output from the underlying process.
$this |
RuntimeException | In case the process is already running |
LogicException | if an idle timeout is set |
Enables fetching output and error output from the underlying process.
$this |
RuntimeException | In case the process is already running |
Returns true in case the output is disabled, false otherwise.
bool |
Returns the current output of the process (STDOUT).
string | The process output |
LogicException | in case the output has been disabled |
LogicException | In case the process is not started |
Returns the output incrementally.
In comparison with the getOutput method which always return the whole output, this one returns the new output since the last call.
string | The process output since the last call |
LogicException | in case the output has been disabled |
LogicException | In case the process is not started |
Returns an iterator to the output of the process, with the output type as keys (Process::OUT/ERR).
int | $flags | A bit field of Process::ITER_* flags |
Generator |
LogicException | in case the output has been disabled |
LogicException | In case the process is not started |
Clears the process output.
$this |
Returns the current error output of the process (STDERR).
string | The process error output |
LogicException | in case the output has been disabled |
LogicException | In case the process is not started |
Returns the errorOutput incrementally.
In comparison with the getErrorOutput method which always return the whole error output, this one returns the new error output since the last call.
string | The process error output since the last call |
LogicException | in case the output has been disabled |
LogicException | In case the process is not started |
Clears the process output.
$this |
Returns the exit code returned by the process.
null|int | The exit status code, null if the Process is not terminated |
RuntimeException | In case --enable-sigchild is activated and the sigchild compatibility mode is disabled |
Returns a string representation for the exit code returned by the process.
This method relies on the Unix exit code status standardization and might not be relevant for other operating systems.
null|string | A string representation for the exit status code, null if the Process is not terminated |
http://tldp.org/LDP/abs/html/exitcodes.html | |
http://en.wikipedia.org/wiki/Unix_signal |
Checks if the process ended successfully.
bool | true if the process ended successfully, false otherwise |
Returns true if the child process has been terminated by an uncaught signal.
It always returns false on Windows.
bool |
RuntimeException | In case --enable-sigchild is activated |
LogicException | In case the process is not terminated |
Returns the number of the signal that caused the child process to terminate its execution.
It is only meaningful if hasBeenSignaled() returns true.
int |
RuntimeException | In case --enable-sigchild is activated |
LogicException | In case the process is not terminated |
Returns true if the child process has been stopped by a signal.
It always returns false on Windows.
bool |
LogicException | In case the process is not terminated |
Returns the number of the signal that caused the child process to stop its execution.
It is only meaningful if hasBeenStopped() returns true.
int |
LogicException | In case the process is not terminated |
Checks if the process is currently running.
bool | true if the process is currently running, false otherwise |
Checks if the process has been started with no regard to the current state.
bool | true if status is ready, false otherwise |
Checks if the process is terminated.
bool | true if process is terminated, false otherwise |
Gets the process status.
The status is one of: ready, started, terminated.
string | The current process status |
Stops the process.
int|float | $timeout | The timeout in seconds |
int | $signal | A POSIX signal to send in case the process has not stop at timeout, default is SIGKILL (9) |
int | The exit-code of the process |
Adds a line to the STDOUT stream.
string | $line | The line to append |
Adds a line to the STDERR stream.
string | $line | The line to append |
Gets the command line to be executed.
string | The command to execute |
Sets the command line to be executed.
string|array | $commandline | The command to execute |
Process | The current Process instance |
Gets the process timeout (max. runtime).
float|null | The timeout in seconds or null if it's disabled |
Gets the process idle timeout (max. time since last output).
float|null | The timeout in seconds or null if it's disabled |
Sets the process timeout (max. runtime).
To disable the timeout, set this value to null.
int|float|null | $timeout | The timeout in seconds |
Process | The current Process instance |
InvalidArgumentException | if the timeout is negative |
Sets the process idle timeout (max. time since last output).
To disable the timeout, set this value to null.
int|float|null | $timeout | The timeout in seconds |
Process | The current Process instance |
LogicException | if the output is disabled |
InvalidArgumentException | if the timeout is negative |
Enables or disables the TTY mode.
bool | $tty | True to enabled and false to disable |
Process | The current Process instance |
RuntimeException | In case the TTY mode is not supported |
Checks if the TTY mode is enabled.
bool | true if the TTY mode is enabled, false otherwise |
Sets PTY mode.
bool | $bool |
Process |
Returns PTY state.
bool |
Gets the working directory.
string|null | The current working directory or null on failure |
Sets the current working directory.
string | $cwd | The new working directory |
Process | The current Process instance |
Gets the environment variables.
array | The current environment variables |
Sets the environment variables.
An environment variable value should be a string. If it is an array, the variable is ignored. If it is false or null, it will be removed when env vars are otherwise inherited.
That happens in PHP when 'argv' is registered into the $_ENV array for instance.
array | $env | The new environment variables |
Process | The current Process instance |
Gets the Process input.
resource|string|Iterator|null | The Process input |
Sets the input.
This content will be passed to the underlying process standard input.
resource|scalar|Traversable|null | $input | The content |
Process | The current Process instance |
LogicException | In case the process is running |
deprecated
Gets the options for proc_open.
array | The current options |
deprecated
Sets the options for proc_open.
array | $options | The new options |
Process | The current Process instance |
deprecated
Gets whether or not Windows compatibility is enabled.
This is true by default.
bool |
deprecated
Sets whether or not Windows compatibility is enabled.
bool | $enhance |
Process | The current Process instance |
deprecated
Returns whether sigchild compatibility mode is activated or not.
bool |
deprecated
Activates sigchild compatibility mode.
Sigchild compatibility mode is required to get the exit code and determine the success of a process when PHP has been compiled with the --enable-sigchild option
bool | $enhance |
Process | The current Process instance |
Sets whether environment variables will be inherited or not.
bool | $inheritEnv |
Process | The current Process instance |
deprecated
Returns whether environment variables will be inherited or not.
bool |
Performs a check between the timeout definition and the time the process started.
In case you run a background process (with the start method), you should trigger this method regularly to ensure the process timeout
ProcessTimedOutException | In case the timeout was reached |
Returns whether PTY is supported on the current operating system.
bool |
© 2004–2017 Fabien Potencier
Licensed under the MIT License.
http://api.symfony.com/4.0/Symfony/Component/Process/Process.html