Coroutine System: exec

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Coroutine\System::exec(string $cmd): array

Parameters

cmd

The Linux command you want to execution on the command line. A shell instruction.

Return

Returns an array which contains information about the command during its execution. If the command failed to execution, then false is returned.

Description

Execute a Linux command within a coroutine context, this method does not block other processes.

Note: You should consider using coroutine hooks because they allow you to use native PHP functions. The Coroutine System API was mainly used before coroutine hooks were implemented. Checkout the supported hook for exec() or shell_exec(): OpenSwoole\Runtime::HOOK_BLOCKING_FUNCTION.


Example

<?php

co::run(function()
{
    $result = OpenSwoole\Coroutine\System::exec("md5sum " . __FILE__);
    var_dump($result);
});

Output:

<?php

array(3) {
  ["code"]=>
  int(0)
  ["signal"]=>
  int(0)
  ["output"]=>
  string(56) "2cbbb45d3dde31c1bad89a5fb60feb79  /root/openswoole.php"
}

We get the status code of the command, the exit signal and the actual contents if the command returned any result.

Last updated on September 21, 2022