Creative Commons License
This work is licensed under a
Creative Commons
Attribution-Share Alike 3.0
Unported License.

Master Symfony2 fundamentals

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Discover the SensioLabs Support

Access to the SensioLabs Competency Center for an exclusive and tailor-made support on Symfony
sensiolabs.com

The Process Component

The Process component executes commands in sub-processes.

Installation

You can install the component in 2 different ways:

Usage

The Process class allows you to execute a command in a sub-process:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
use Symfony\Component\Process\Process;

$process = new Process('ls -lsa');
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    throw new \RuntimeException($process->getErrorOutput());
}

print $process->getOutput();

The component takes care of the subtle differences between the different platforms when executing the command.

New in version 2.2: The getIncrementalOutput() and getIncrementalErrorOutput() methods were added in Symfony 2.2.

The getOutput() method always return the whole content of the standard output of the command and getErrorOutput() the content of the error output. Alternatively, the getIncrementalOutput() and getIncrementalErrorOutput() methods returns the new outputs since the last call.

New in version 2.4: The clearOutput() and clearErrorOutput() methods were introduced in Symfony 2.4.

The clearOutput() method clears the contents of the output and clearErrorOutput() clears the contents of the error output.

Getting real-time Process Output

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 (Process::ERR === $type) {
        echo 'ERR > '.$buffer;
    } else {
        echo 'OUT > '.$buffer;
    }
});

Running Processes Asynchronously

You can also start the subprocess and then let it run asynchronously, retrieving output and the status in your main process whenever you need it. Use the start() method to start an asynchronous process, the isRunning() method to check if the process is done and the getOutput() method to get the output:

1
2
3
4
5
6
7
8
$process = new Process('ls -lsa');
$process->start();

while ($process->isRunning()) {
    // waiting for process to finish
}

echo $process->getOutput();

You can also wait for a process to end if you started it asynchronously and are done doing other stuff:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$process = new Process('ls -lsa');
$process->start();

// ... do other things

$process->wait(function ($type, $buffer) {
    if (Process::ERR === $type) {
        echo 'ERR > '.$buffer;
    } else {
        echo 'OUT > '.$buffer;
    }
});

Note

The wait() method is blocking, which means that your code will halt at this line until the external process is completed.

Stopping a Process

New in version 2.3: The signal parameter of the stop method was introduced in Symfony 2.3.

Any asynchronous process can be stopped at any time with the stop() method. This method takes two arguments : a timeout and a signal. Once the timeout is reached, the signal is sent to the running process. The default signal sent to a process is SIGKILL. Please read the signal documentation below to find out more about signal handling in the Process component:

1
2
3
4
5
6
$process = new Process('ls -lsa');
$process->start();

// ... do other things

$process->stop(3, SIGINT);

Executing PHP Code in Isolation

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();

To make your code work better on all platforms, you might want to use the ProcessBuilder class instead:

1
2
3
4
use Symfony\Component\Process\ProcessBuilder;

$builder = new ProcessBuilder(array('ls', '-lsa'));
$builder->getProcess()->run();

New in version 2.3: The ProcessBuilder::setPrefix method was introduced in Symfony 2.3.

In case you are building a binary driver, you can use the setPrefix() method to prefix all the generated process commands.

The following example will generate two process commands for a tar binary adapter:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
use Symfony\Component\Process\ProcessBuilder;

$builder = new ProcessBuilder();
$builder->setPrefix('/usr/bin/tar');

// '/usr/bin/tar' '--list' '--file=archive.tar.gz'
echo $builder
    ->setArguments(array('--list', '--file=archive.tar.gz'))
    ->getProcess()
    ->getCommandLine();

// '/usr/bin/tar' '-xzf' 'archive.tar.gz'
echo $builder
    ->setArguments(array('-xzf', 'archive.tar.gz'))
    ->getProcess()
    ->getCommandLine();

Process Timeout

You can limit the amount of time a process takes to complete by setting a timeout (in seconds):

1
2
3
4
5
use Symfony\Component\Process\Process;

$process = new Process('ls -lsa');
$process->setTimeout(3600);
$process->run();

If the timeout is reached, a RuntimeException is thrown.

For long running commands, it is your responsibility to perform the timeout check regularly:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$process->setTimeout(3600);
$process->start();

while ($condition) {
    // ...

    // check if the timeout is reached
    $process->checkTimeout();

    usleep(200000);
}

Process Idle Timeout

New in version 2.4: The setIdleTimeout() method was added in Symfony 2.4.

In contrast to the timeout of the previous paragraph, the idle timeout only considers the time since the last output was produced by the process:

1
2
3
4
5
6
use Symfony\Component\Process\Process;

$process = new Process('something-with-variable-runtime');
$process->setTimeout(3600);
$process->setIdleTimeout(60);
$process->run();

In the case above, a process is considered timed out, when either the total runtime exceeds 3600 seconds, or the process does not produce any output for 60 seconds.

Process Signals

New in version 2.3: The signal method was introduced in Symfony 2.3.

When running a program asynchronously, you can send it posix signals with the signal() method:

1
2
3
4
5
6
7
use Symfony\Component\Process\Process;

$process = new Process('find / -name "rabbit"');
$process->start();

// will send a SIGKILL to the process
$process->signal(SIGKILL);

Caution

Due to some limitations in PHP, if you're using signals with the Process component, you may have to prefix your commands with exec. Please read Symfony Issue#5759 and PHP Bug#39992 to understand why this is happening.

POSIX signals are not available on Windows platforms, please refer to the PHP documentation for available signals.

Process Pid

New in version 2.3: The getPid method was introduced in Symfony 2.3.

You can access the pid of a running process with the getPid() method.

1
2
3
4
5
6
use Symfony\Component\Process\Process;

$process = new Process('/usr/bin/php worker.php');
$process->start();

$pid = $process->getPid();

Caution

Due to some limitations in PHP, if you want to get the pid of a symfony Process, you may have to prefix your commands with exec. Please read Symfony Issue#5759 to understand why this is happening.