- How do I run an external command in Laravel with Symfony Process?
- Create a Process instance with an array command such as new Process(['ls', '-la']), call run(), then read getOutput() or getErrorOutput(). Using the array form avoids shell interpolation and is safer than exec or shell_exec. You can use it directly in controllers, jobs, Artisan commands, or service classes.
- How do I install symfony/process in a Laravel project?
- Install it with Composer using composer require symfony/process. Symfony Process is framework-agnostic and has no Laravel-specific dependencies, so it works as a normal package in a Laravel app. Laravel may already pull in Symfony components transitively, but require it directly if your code depends on it.
- Which Laravel and PHP versions support symfony/process?
- Symfony Process is not tied to a Laravel release, but each Symfony Process version has its own PHP requirement and should match your app's Composer constraints. Choose the release line that fits your PHP version and Laravel dependency lock, and test before upgrading major versions. For apps on the Symfony 5.4 LTS line, 5.4.54 is the security patch to prioritize.
- Does Symfony Process work with Laravel queues and the scheduler?
- Yes. You can use Process inside queued jobs, scheduled commands, and Artisan commands to run CLI tools or deployment scripts. Keep process timeouts aligned with Laravel queue worker timeouts so a hanging subprocess does not stall workers. For long-running work, use start() and isRunning() to avoid blocking where appropriate.
- How do I capture stdout and stderr from a Symfony Process?
- After run(), use getOutput() for standard output and getErrorOutput() for standard error. A non-zero exit code can be turned into a ProcessFailedException with mustRun(), which includes useful debugging context. For real-time logging or progress, pass a callback to run() and handle output chunks as they arrive.
- How do I set environment variables and the working directory for a process?
- Pass env values through the constructor or setEnv(), and use setWorkingDirectory() to control where the command runs. In Symfony Process 5.4.54, invalid environment variable names and non-scalar values are silently ignored instead of propagated, so audit env arrays for arrays or objects. This hardening is intentional but can hide misconfiguration if you relied on loud failures.
- How do I stop a Symfony Process from hanging?
- Call setTimeout() with a sensible limit before running the process and catch ProcessTimedOutException where needed. Timeouts are especially important for external tools, network calls, and CI or deployment scripts. Combine timeouts with clear error handling and logging so failed subprocesses are visible in Laravel logs.
- What security issue does symfony/process 5.4.54 fix?
- Version 5.4.54 is a security release that stops leaking CGI/FastCGI request-context variables to subprocesses, which matters for Laravel apps running under PHP-FPM that shell out. It also ignores invalid env names and non-scalar env values, and uses an absolute path for the Windows cmd.exe fallback. Treat it as a patch-level upgrade from 5.4.51 with no Process signature changes, then regression-test workflows that depended on inherited REQUEST_ or CGI variables.
- Does Symfony Process work on Windows?
- Yes, Symfony Process supports Windows and Unix-like systems, but escaping, environment variable limits, and shell behavior differ by platform. The 5.4.54 security patch makes the cmd.exe fallback use an absolute path, reducing binary hijacking risk on Windows hosts. Test Windows CI runners and developer machines if you pass large env arrays or complex arguments.
- How should I test Laravel code that uses Symfony Process?
- Wrap process creation behind a small service, factory, or interface so tests can substitute a fake instead of running real binaries. Assert the command arguments, environment, working directory, output handling, and failure paths in unit tests. Keep a few integration tests with harmless commands to verify real process behavior without making the suite slow or environment-dependent.