You are browsing the Symfony 4 documentation, which changes significantly from Symfony 3.x. If your app doesn't use Symfony 4 yet, browse the Symfony 3.4 documentation.

The Process Component

The Process Component

The Process component executes commands in sub-processes.

Installation

1
$ composer require symfony/process

Alternatively, you can clone the https://github.com/symfony/process repository.

Note

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

Usage

The Process class executes a command in a sub-process, taking care of the differences between operating system and escaping arguments to prevent security issues. It replaces PHP functions like exec, passthru, shell_exec and system:

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

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

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

echo $process->getOutput();

Tip

In addition to passing the command binary and its arguments as a string, you can also pass them as an array, which is useful when building a complex command programmatically:

1
2
3
4
5
6
// traditional string based commands
$builder = new Process('ls -lsa');
// same example but using an array
$builder = new Process(array('ls', '-lsa'));
// the array can contain any number of arguments and options
$builder = new Process(array('ls', '-l', '-s', '-a'));

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

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

You can also use the Process class with the foreach construct to get the output while it is generated. By default, the loop waits for new output before going to the next iteration:

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

foreach ($process as $type => $data) {
    if ($process::OUT === $type) {
        echo "\nRead from stdout: ".$data;
    } else { // $process::ERR === $type
        echo "\nRead from stderr: ".$data;
    }
}

Tip

The Process component internally uses a PHP iterator to get the output while it is generated. That iterator is exposed via the getIterator() method to allow customizing its behavior:

1
2
3
4
5
6
$process = new Process(array('ls', '-lsa'));
$process->start();
$iterator = $process->getIterator($process::ITER_SKIP_ERR | $process::ITER_KEEP_OUTPUT);
foreach ($iterator as $data) {
    echo $data."\n";
}

The mustRun() method is identical to run(), except that it will throw a ProcessFailedException if the process couldn't be executed successfully (i.e. the process exited with a non-zero code):

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

$process = new Process(array('ls', '-lsa'));

try {
    $process->mustRun();

    echo $process->getOutput();
} catch (ProcessFailedException $exception) {
    echo $exception->getMessage();
}

Tip

New in version 3.3: The ability to define commands as arrays of arguments was introduced in Symfony 3.3.

Using array of arguments is the recommended way to define commands. This saves you from any escaping and allows sending signals seamlessly (e.g. to stop processes before completion.):

$process = new Process(array('/path/command', '--flag', 'arg 1', 'etc.'));

If you need to use stream redirections, conditional execution, or any other feature provided by the shell of your operating system, you can also define commands as strings.

Please note that each OS provides a different syntax for their command-lines so that it becomes your responsibility to deal with escaping and portability.

To provide any variable arguments to command-line string, pass them as environment variables using the second argument of the run(), mustRun() or start() methods. Referencing them is also OS-dependent:

1
2
3
4
5
6
7
8
// On Unix-like OSes (Linux, macOS)
$process = new Process('echo "$MESSAGE"');

// On Windows
$process = new Process('echo "!MESSAGE!"');

// On both Unix-like and Windows
$process->run(null, array('MESSAGE' => 'Something to 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(array('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(array('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
$process = new Process(array('ls', '-lsa'));
$process->start();

// ... do other things

$process->wait();

// ... do things after the process has finished

Note

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

Note

If a Response is sent before a child process had a chance to complete, the server process will be killed (depending on your OS). It means that your task will be stopped right away. Running an asynchronous process is not the same as running a process that survives its parent process.

If you want your process to survive the request/response cycle, you can take advantage of the kernel.terminate event, and run your command synchronously inside this event. Be aware that kernel.terminate is called only if you use PHP-FPM.

Caution

Beware also that if you do that, the said PHP-FPM process will not be available to serve any new request until the subprocess is finished. This means you can quickly block your FPM pool if you're not careful enough. That is why it's generally way better not to do any fancy things even after the request is sent, but to use a job queue instead.

wait() takes one optional argument: a callback that is called repeatedly whilst the process is still running, passing in the output and its type:

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

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

Streaming to the Standard Input of a Process

Before a process is started, you can specify its standard input using either the setInput() method or the 4th argument of the constructor. The provided input can be a string, a stream resource or a Traversable object:

$process = new Process('cat');
$process->setInput('foobar');
$process->run();

When this input is fully written to the subprocess standard input, the corresponding pipe is closed.

In order to write to a subprocess standard input while it is running, the component provides the InputStream class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
$input = new InputStream();
$input->write('foo');

$process = new Process(array('cat'));
$process->setInput($input);
$process->start();

// ... read process output or do other things

$input->write('bar');
$input->close();

$process->wait();

// will echo: foobar
echo $process->getOutput();

The write() method accepts scalars, stream resources or Traversable objects as argument. As shown in the above example, you need to explicitly call the close() method when you are done writing to the standard input of the subprocess.

Using PHP Streams as the Standard Input of a Process

The input of a process can also be defined using PHP streams:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$stream = fopen('php://temporary', 'w+');

$process = new Process(array('cat'));
$process->setInput($stream);
$process->start();

fwrite($stream, 'foo');

// ... read process output or do other things

fwrite($stream, 'bar');
fclose($stream);

$process->wait();

// will echo: 'foobar'
echo $process->getOutput();

Stopping a Process

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(array('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
    <?= 'Hello World' ?>
EOF
);
$process->run();

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(array('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

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(array('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

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(array('find', '/', '-name', 'rabbit'));
$process->start();

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

Process Pid

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(array('/usr/bin/php', 'worker.php'));
$process->start();

$pid = $process->getPid();

Disabling Output

As standard output and error output are always fetched from the underlying process, it might be convenient to disable output in some cases to save memory. Use disableOutput() and enableOutput() to toggle this feature:

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

$process = new Process(array('/usr/bin/php', 'worker.php'));
$process->disableOutput();
$process->run();

Caution

You cannot enable or disable the output while the process is running.

If you disable the output, you cannot access getOutput(), getIncrementalOutput(), getErrorOutput(), getIncrementalErrorOutput() or setIdleTimeout().

However, it is possible to pass a callback to the start, run or mustRun methods to handle process output in a streaming fashion.

Finding the Executable PHP Binary

This component also provides a utility class called PhpExecutableFinder which returns the absolute path of the executable PHP binary available on your server:

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

$phpBinaryFinder = new PhpExecutableFinder();
$phpBinaryPath = $phpBinaryFinder->find();
// $phpBinaryPath = '/usr/local/bin/php' (the result will be different on your computer)

Checking for TTY Support

Another utility provided by this component is a method called isTtySupported() which returns whether TTY is supported on the current operating system:

use Symfony\Component\Process\Process;

$process = (new Process())->setTty(Process::isTtySupported());

New in version 4.1: The isTtySupported() method was introduced in Symfony 4.1.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.