class Process

Process is a thin wrapper around proc_* functions to ease start independent PHP processes.

Constants

ERR

OUT

STATUS_READY

STATUS_STARTED

STATUS_TERMINATED

STDIN

STDOUT

STDERR

TIMEOUT_PRECISION

Properties

static array $exitCodes Exit codes translation table.

Methods

__construct(string $commandline, string|null $cwd = null, array $env = null, string|null $stdin = null, integer|float|null $timeout = 60, array $options = array())

Constructor.

__destruct()

No description

__clone()

No description

integer
run(callback|null $callback = null)

Runs the process.

start(callback|null $callback = null)

Starts the process and returns after sending the STDIN.

restart(callable $callback = null)

Restarts the process.

integer
wait(callback|null $callback = null)

Waits for the process to terminate.

integer|null
getPid()

Returns the Pid (process identifier), if applicable.

signal(integer $signal)

Sends a POSIX signal to the process.

string
getOutput()

Returns the current output of the process (STDOUT).

string
getIncrementalOutput()

Returns the output incrementally.

string
getErrorOutput()

Returns the current error output of the process (STDERR).

string
getIncrementalErrorOutput()

Returns the errorOutput incrementally.

integer
getExitCode()

Returns the exit code returned by the process.

string
getExitCodeText()

Returns a string representation for the exit code returned by the process.

Boolean
isSuccessful()

Checks if the process ended successfully.

Boolean
hasBeenSignaled()

Returns true if the child process has been terminated by an uncaught signal.

integer
getTermSignal()

Returns the number of the signal that caused the child process to terminate its execution.

Boolean
hasBeenStopped()

Returns true if the child process has been stopped by a signal.

integer
getStopSignal()

Returns the number of the signal that caused the child process to stop its execution.

Boolean
isRunning()

Checks if the process is currently running.

Boolean
isStarted()

Checks if the process has been started with no regard to the current state.

Boolean
isTerminated()

Checks if the process is terminated.

string
getStatus()

Gets the process status.

integer
stop(integer|float $timeout = 10, integer $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.

setCommandLine(string $commandline)

Sets the command line to be executed.

float|null
getTimeout()

Gets the process timeout.

setTimeout(integer|float|null $timeout)

Sets the process timeout.

setTty(boolean $tty)

Enables or disables the TTY mode.

Boolean
isTty()

Checks if the TTY mode is enabled.

string|null
getWorkingDirectory()

Gets the working directory.

setWorkingDirectory(string $cwd)

Sets the current working directory.

array
getEnv()

Gets the environment variables.

setEnv(array $env)

Sets the environment variables.

string|null
getStdin()

Gets the contents of STDIN.

setStdin(string|null $stdin)

Sets the contents of STDIN.

array
getOptions()

Gets the options for proc_open.

setOptions(array $options)

Sets the options for proc_open.

Boolean
getEnhanceWindowsCompatibility()

Gets whether or not Windows compatibility is enabled.

setEnhanceWindowsCompatibility(Boolean $enhance)

Sets whether or not Windows compatibility is enabled.

Boolean
getEnhanceSigchildCompatibility()

Returns whether sigchild compatibility mode is activated or not.

setEnhanceSigchildCompatibility(Boolean $enhance)

Activates sigchild compatibility mode.

checkTimeout()

Performs a check between the timeout definition and the time the process started.

Details

at line line 133
__construct(string $commandline, string|null $cwd = null, array $env = null, string|null $stdin = null, integer|float|null $timeout = 60, array $options = array())

Constructor.

Parameters

string $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 inherit
string|null $stdin The STDIN content
integer|float|null $timeout The timeout in seconds or null to disable
array $options An array of options for proc_open

Exceptions

RuntimeException When proc_open is not installed

at line line 163
__destruct()

at line line 169
__clone()

at line line 193
integer run(callback|null $callback = null)

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.

Parameters

callback|null $callback A PHP callback to run whenever there is some output available on STDOUT or STDERR

Return Value

integer The exit status code

Exceptions

RuntimeException When process can't be launch or is stopped

at line line 221
start(callback|null $callback = null)

Starts the process and returns after sending the 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. If there is no callback passed, the wait() method can be called with true as a second parameter then the callback will get all data occurred in (and since) the start call.

Parameters

callback|null $callback A PHP callback to run whenever there is some output available on STDOUT or STDERR

Exceptions

RuntimeException When process can't be launch or is stopped
RuntimeException When process is already running

at line line 276
Process restart(callable $callback = null)

Restarts the process.

Be warned that the process is cloned before being started.

Parameters

callable $callback A PHP callback to run whenever there is some output available on STDOUT or STDERR

Return Value

Process The new process

Exceptions

RuntimeException When process can't be launch or is stopped
RuntimeException When process is already running

See also

start()

at line line 302
integer wait(callback|null $callback = null)

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.

Parameters

callback|null $callback A valid PHP callback

Return Value

integer The exitcode of the process

Exceptions

RuntimeException When process timed out
RuntimeException When process stopped after receiving signal

at line line 338
integer|null getPid()

Returns the Pid (process identifier), if applicable.

Return Value

integer|null The process id if running, null otherwise

Exceptions

RuntimeException In case --enable-sigchild is activated

at line line 359
Process signal(integer $signal)

Sends a POSIX signal to the process.

Parameters

integer $signal A valid POSIX signal (see http://www.php.net/manual/en/pcntl.constants.php)

Return Value

Process

Exceptions

LogicException In case the process is not running
RuntimeException In case --enable-sigchild is activated
RuntimeException In case of failure

at line line 383
string getOutput()

Returns the current output of the process (STDOUT).

Return Value

string The process output

at line line 398
string getIncrementalOutput()

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.

Return Value

string The process output since the last call

at line line 415
string getErrorOutput()

Returns the current error output of the process (STDERR).

Return Value

string The process error output

at line line 431
string getIncrementalErrorOutput()

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.

Return Value

string The process error output since the last call

at line line 450
integer getExitCode()

Returns the exit code returned by the process.

Return Value

integer The exit status code

Exceptions

RuntimeException In case --enable-sigchild is activated and the sigchild compatibility mode is disabled

at line line 472
string getExitCodeText()

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.

Return Value

string A string representation for the exit status code

See also

http://tldp.org/LDP/abs/html/exitcodes.html
http://en.wikipedia.org/wiki/Unix_signal

at line line 486
Boolean isSuccessful()

Checks if the process ended successfully.

Return Value

Boolean true if the process ended successfully, false otherwise

at line line 502
Boolean hasBeenSignaled()

Returns true if the child process has been terminated by an uncaught signal.

It always returns false on Windows.

Return Value

Boolean

Exceptions

RuntimeException In case --enable-sigchild is activated

at line line 524
integer getTermSignal()

Returns the number of the signal that caused the child process to terminate its execution.

It is only meaningful if hasBeenSignaled() returns true.

Return Value

integer

Exceptions

RuntimeException In case --enable-sigchild is activated

at line line 544
Boolean hasBeenStopped()

Returns true if the child process has been stopped by a signal.

It always returns false on Windows.

Return Value

Boolean

at line line 560
integer getStopSignal()

Returns the number of the signal that caused the child process to stop its execution.

It is only meaningful if hasBeenStopped() returns true.

Return Value

integer

at line line 572
Boolean isRunning()

Checks if the process is currently running.

Return Value

Boolean true if the process is currently running, false otherwise

at line line 588
Boolean isStarted()

Checks if the process has been started with no regard to the current state.

Return Value

Boolean true if status is ready, false otherwise

at line line 598
Boolean isTerminated()

Checks if the process is terminated.

Return Value

Boolean true if process is terminated, false otherwise

at line line 612
string getStatus()

Gets the process status.

The status is one of: ready, started, terminated.

Return Value

string The current process status

at line line 629
integer stop(integer|float $timeout = 10, integer $signal = null)

Stops the process.

Parameters

integer|float $timeout The timeout in seconds
integer $signal A POSIX signal to send in case the process has not stop at timeout, default is SIGKILL

Return Value

integer The exit-code of the process

Exceptions

RuntimeException if the process got signaled

at line line 660
addOutput(string $line)

Adds a line to the STDOUT stream.

Parameters

string $line The line to append

at line line 670
addErrorOutput(string $line)

Adds a line to the STDERR stream.

Parameters

string $line The line to append

at line line 680
string getCommandLine()

Gets the command line to be executed.

Return Value

string The command to execute

at line line 692
Process setCommandLine(string $commandline)

Sets the command line to be executed.

Parameters

string $commandline The command to execute

Return Value

Process The current Process instance

at line line 704
float|null getTimeout()

Gets the process timeout.

Return Value

float|null The timeout in seconds or null if it's disabled

at line line 720
Process setTimeout(integer|float|null $timeout)

Sets the process timeout.

To disable the timeout, set this value to null.

Parameters

integer|float|null $timeout The timeout in seconds

Return Value

Process The current Process instance

Exceptions

InvalidArgumentException if the timeout is negative

at line line 742
Process setTty(boolean $tty)

Enables or disables the TTY mode.

Parameters

boolean $tty True to enabled and false to disable

Return Value

Process The current Process instance

at line line 754
Boolean isTty()

Checks if the TTY mode is enabled.

Return Value

Boolean true if the TTY mode is enabled, false otherwise

at line line 764
string|null getWorkingDirectory()

Gets the working directory.

Return Value

string|null The current working directory or null on failure

at line line 782
Process setWorkingDirectory(string $cwd)

Sets the current working directory.

Parameters

string $cwd The new working directory

Return Value

Process The current Process instance

at line line 794
array getEnv()

Gets the environment variables.

Return Value

array The current environment variables

at line line 812
Process setEnv(array $env)

Sets the environment variables.

An environment variable value should be a string. If it is an array, the variable is ignored.

That happens in PHP when 'argv' is registered into the $_ENV array for instance.

Parameters

array $env The new environment variables

Return Value

Process The current Process instance

at line line 830
string|null getStdin()

Gets the contents of STDIN.

Return Value

string|null The current contents

at line line 842
Process setStdin(string|null $stdin)

Sets the contents of STDIN.

Parameters

string|null $stdin The new contents

Return Value

Process The current Process instance

at line line 854
array getOptions()

Gets the options for proc_open.

Return Value

array The current options

at line line 866
Process setOptions(array $options)

Sets the options for proc_open.

Parameters

array $options The new options

Return Value

Process The current Process instance

at line line 880
Boolean getEnhanceWindowsCompatibility()

Gets whether or not Windows compatibility is enabled.

This is true by default.

Return Value

Boolean

at line line 892
Process setEnhanceWindowsCompatibility(Boolean $enhance)

Sets whether or not Windows compatibility is enabled.

Parameters

Boolean $enhance

Return Value

Process The current Process instance

at line line 904
Boolean getEnhanceSigchildCompatibility()

Returns whether sigchild compatibility mode is activated or not.

Return Value

Boolean

at line line 920
Process setEnhanceSigchildCompatibility(Boolean $enhance)

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

Parameters

Boolean $enhance

Return Value

Process The current Process instance

at line line 935
checkTimeout()

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

Exceptions

RuntimeException In case the timeout was reached