Processes
Introduction
Laravel provides an expressive, minimal API around the Symfony Process component, allowing you to conveniently invoke external processes from your Laravel application. Laravel's process features are focused on the most common use cases and a wonderful developer experience.
Invoking Processes
To invoke a process, you may use the run
and start
methods offered by the Process
facade. The run
method will invoke a process and wait for the process to finish executing, while the start
method is used for asynchronous process execution. We'll examine both approaches within this documentation. First, let's examine how to invoke a basic, synchronous process and inspect its result:
use Illuminate\Support\Facades\Process;
$result = Process::run('ls -la');
return $result->output();
Of course, the Illuminate\Contracts\Process\ProcessResult
instance returned by the run
method offers a variety of helpful methods that may be used to inspect the process result:
$result = Process::run('ls -la');
$result->successful();
$result->failed();
$result->exitCode();
$result->output();
$result->errorOutput();
Throwing Exceptions
If you have a process result and would like to throw an instance of Illuminate\Process\Exceptions\ProcessFailedException
if the exit code is greater than zero (thus indicating failure), you may use the throw
and throwIf
methods. If the process did not fail, the process result instance will be returned:
$result = Process::run('ls -la')->throw();
$result = Process::run('ls -la')->throwIf($condition);
Process Options
Of course, you may need to customize the behavior of a process before invoking it. Thankfully, Laravel allows you to tweak a variety of process features, such as the working directory, timeout, and environment variables.
Working Directory Path
You may use the path
method to specify the working directory of the process. If this method is not invoked, the process will inherit the working directory of the currently executing PHP script:
$result = Process::path(__DIR__)->run('ls -la');
Input
You may provide input via the "standard input" of the process using the input
method:
$result = Process::input('Hello World')->run('cat');
Timeouts
By default, processes will throw an instance of Illuminate\Process\Exceptions\ProcessTimedOutException
after executing for more than 60 seconds. However, you can customize this behavior via the timeout
method:
$result = Process::timeout(120)->run('bash import.sh');
Or, if you would like to disable the process timeout entirely, you may invoke the forever
method:
$result = Process::forever()->run('bash import.sh');
The idleTimeout
method may be used to specify the maximum number of seconds the process may run without returning any output:
$result = Process::timeout(60)->idleTimeout(30)->run('bash import.sh');
Environment Variables
Environment variables may be provided to the process via the env
method. The invoked process will also inherit all of the environment variables defined by your system:
$result = Process::forever()
->env(['IMPORT_PATH' => __DIR__])
->run('bash import.sh');
If you wish to remove an inherited environment variable from the invoked process, you may provide that environment variable with a value of false
:
$result = Process::forever()
->env(['LOAD_PATH' => false])
->run('bash import.sh');
TTY Mode
The tty
method may be used to enable TTY mode for your process. TTY mode connects the input and output of the process to the input and output of your program, allowing your process to open an editor like Vim or Nano as a process:
Process::forever()->tty()->run('vim');