Introduction
Laravel’sProcess facade is a thin wrapper around the Symfony Process component. It provides a clean, expressive API for invoking external processes from your application.
Invoking processes
UseProcess::run() to execute a command synchronously and wait for it to finish.
ProcessResult instance exposes several inspection methods:
Throwing on failure
To throw anIlluminate\Process\Exceptions\ProcessFailedException when a process fails, use throw() or throwIf(). If the process succeeded, the ProcessResult instance is returned.
Process options
Working directory
Usepath() to set the working directory. If omitted, the process inherits the directory of the currently executing PHP script.
Standard input
Pass data to the process via standard input usinginput().
Timeouts
Processes time out after 60 seconds by default, throwing aProcessTimedOutException. Override with timeout().
CarbonInterval helper function:
forever().
idleTimeout().
Environment variables
Provide environment variables withenv(). The process also inherits all system environment variables.
false to remove an inherited variable.
Disabling output
When you don’t need the output, callquietly() to conserve memory.
Real-time output
Pass a closure as the second argument torun() to receive output as it is produced.
Pipelines
Process::pipe() lets you chain commands so that the output of one becomes the input of the next. The result of the last command in the pipeline is returned.
as() to identify each process’s output in the closure.
Asynchronous processes
Process::start() launches a process without waiting for it to finish, allowing your application to do other work in the meantime.
Process IDs and signals
Retrieve the OS-assigned process ID withid().
signal().
Asynchronous output
While the process is running, uselatestOutput() and latestErrorOutput() to read new output since the last check.
waitUntil().
Checking for timeouts
CallensureNotTimedOut() inside a loop to throw a timeout exception if the process has exceeded its timeout.
Concurrent processes
Process::pool() runs multiple processes in parallel.
Process::concurrently() is a shorthand that starts the pool and immediately waits for all results.
Naming pool processes
Useas() to assign string keys and access results by name.
Testing
Faking processes
CallProcess::fake() to prevent any real processes from running during tests.
Faking specific processes
Use command patterns as keys to fake individual commands differently.Faking sequences
Return different results for repeated calls to the same command.Assertions
Preventing stray processes
CallProcess::preventStrayProcesses() to throw an exception when a process runs without a matching fake.
Related pages
Artisan console
Create custom commands that invoke processes
Queues
Compare with queue-based async processing