Table of Contents
Questions & Feedback
Found a typo or an error?
Want to improve this document? Edit it.
Need support or have a technical question?
Post to the user mailing-list.
Master Symfony2 fundamentals
Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com
Symfony hosting done right
ServerGrove, outstanding support at the right price for your Symfony hosting needs.
servergrove.com
Discover the SensioLabs Support
Access to the SensioLabs Competency Center for an exclusive and tailor-made support on Symfony
sensiolabs.com
2.0 version
The Process Component
Caution: You are browsing the documentation for Symfony version 2.0 which is not maintained anymore.
If some of your projects are still using this version, consider upgrading.
The Process Component¶
The Process Component executes commands in sub-processes.
Installation¶
You can install the component in many different ways:
- Use the official Git repository (https://github.com/symfony/Process);
- Install it via Composer (
symfony/processon Packagist).
Usage¶
The Process class allows you to execute
a command in a sub-process:
1 2 3 4 5 6 7 8 9 10 | use Symfony\Component\Process\Process;
$process = new Process('ls -lsa');
$process->setTimeout(3600);
$process->run();
if (!$process->isSuccessful()) {
throw new \RuntimeException($process->getErrorOutput());
}
print $process->getOutput();
|
The run() method takes care
of the subtle differences between the different platforms when executing the
command.
When executing a long running command (like rsync-ing files to a remote
server), you can give feedback to the end user in real-time by passing an
anonymous function to the
run() method:
1 2 3 4 5 6 7 8 9 10 | use Symfony\Component\Process\Process;
$process = new Process('ls -lsa');
$process->run(function ($type, $buffer) {
if ('err' === $type) {
echo 'ERR > '.$buffer;
} else {
echo 'OUT > '.$buffer;
}
});
|
If you want to execute some PHP code in isolation, use the PhpProcess
instead:
1 2 3 4 5 6 7 | use Symfony\Component\Process\PhpProcess;
$process = new PhpProcess(<<<EOF
<?php echo 'Hello World'; ?>
EOF
);
$process->run();
|





is a trademark of Fabien Potencier. All rights reserved.