- How do I replace shell_exec() or exec() with Symfony Process in Laravel?
- Use `Process::fromShellCommandline()` to escape arguments safely, then call `run()` to execute. For example, `Process::fromShellCommandline('git pull')->run()` avoids shell injection risks. Always check `$process->isSuccessful()` to handle failures. The API is simpler than `proc_open()` and more secure than raw shell calls.
- Can Symfony Process handle async subprocesses in Laravel Queues?
- Yes. Dispatch a job with `Process::fromShellCommandline()` and `run()` in the handle method. For non-blocking execution, use Laravel’s queue system (e.g., `dispatch(new RunProcessJob($command))`). The process runs in the background, and you can stream output later via `getOutput()`.
- How do I capture real-time stdout/stderr output in a Laravel controller?
- Use `Process::fromShellCommandline()->mustRun()` and iterate over `getOutput()` or `getErrorOutput()` line by line. For streaming, attach a callback to `run()` with `on('progress', fn($type, $buffer) => ...)`. This works for progress bars or live logs in web responses.
- What Laravel versions and PHP versions does Symfony Process support?
- Symfony Process supports PHP 7.2.5+ and Laravel 5.1+. For Laravel 8+, use Symfony Process v6.0+ for PHP 8.4+ features. Check the [Symfony docs](https://symfony.com/doc/current/components/process.html) for version-specific quirks, like Windows environment variable limits (32KB max).
- How do I set timeouts or kill hung processes in Laravel?
- Pass a timeout in seconds to `run()` (e.g., `$process->run(null, ['timeout' => 30])`). To force-kill, use `Process::fromShellCommandline()->setTimeout(30)->mustRun()` and handle `ProcessFailedException`. For signals, use `setSignal()` (e.g., `SIGTERM`) before running.
- Is Symfony Process safe for user-provided commands in Laravel?
- Yes, but validate inputs strictly. Use `Process::fromShellCommandline()` with escaped arguments (never pass raw user input to `exec()`). Reject commands with shell metacharacters (e.g., `;`, `|`) or environment variables exceeding platform limits (e.g., Windows’ 32KB). Always sanitize before constructing the process.
- How do I debug a subprocess that fails silently in Laravel?
- Enable verbose output with `Process::fromShellCommandline()->setTimeout(null)->run()` and check `$process->getErrorOutput()`. For real-time debugging, use `on('error', fn($type, $buffer) => log($buffer))`. If the process hangs, check `hasSystemCallBeenInterrupted()` for platform-specific issues (e.g., Windows PTY limitations).
- Can I use Symfony Process to run Python/Node.js scripts in Laravel?
- Absolutely. Use `Process::fromShellCommandline('python script.py')` or `Process::fromShellCommandline('node app.js')`. For async execution, wrap it in a Laravel job. Stream output to users via `getOutput()` or log it with `on('progress')`. This bridges PHP with polyglot tools seamlessly.
- What’s the difference between `run()` and `mustRun()` in Symfony Process?
- `run()` executes the command and returns immediately, while `mustRun()` throws `ProcessFailedException` if the command fails (exit code ≠ 0). Use `run()` for background jobs where failures are handled later, and `mustRun()` for critical tasks (e.g., migrations) where you need to abort on errors.
- Are there alternatives to Symfony Process for running commands in Laravel?
- Yes, but Symfony Process is the most robust. Alternatives include `exec()`, `shell_exec()` (unsafe), or `proc_open()` (complex). For Laravel-specific tools, consider `spatie/laravel-activitylog` (for logging) or `laravel/excel` (for CLI-driven Excel tasks). However, Symfony Process offers unmatched safety, cross-platform support, and real-time output handling.