Laravel's Process Facade: Safer Shell Work Without a Shell Wrapper
Laravel 10 added a first-party API for starting operating-system processes. It is useful when an application needs to call a converter, run a maintenance script, or invoke a tool that has no suitable PHP library. The key improvement is not shorter syntax; it is a consistent place to define input, timeouts, environment, and result handling.
use Illuminate\Support\Facades\Process;
$result = Process::timeout(30)->run([
'php', 'artisan', 'reports:build', '--format=json',
]);
if ($result->failed()) {
report(new RuntimeException($result->errorOutput()));
}
Prefer argument arrays over concatenated command strings. A user-supplied filename should be data, not shell syntax. Validate it against the application’s own rules, pass it as an argument, and avoid building a command with string interpolation. Set a realistic timeout: a web request should not wait indefinitely for a child process. Long-running work belongs on a queue or dedicated worker.
The result object exposes exit status, standard output, and standard error. Treat output as untrusted input too. Do not return raw command output to a browser, and do not assume a zero exit code proves the generated file is valid. Check the expected artifact and its size or format before using it.
The API also supports fakes, making process-dependent behavior testable without actually launching external programs. For fan-out work, process pools can run independent commands concurrently; cap concurrency according to CPU and memory limits instead of assuming more parallel work is always faster.
Good fit: deterministic, bounded tasks that need a local executable. Poor fit: shelling out to reproduce functionality already available in PHP, or using a request worker as a general job runner.
The Laravel 10 release notes document the Process facade, concurrent pools, and testing support.