Swoole Coroutine System: exec

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\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(): SWOOLE_HOOK_BLOCKING_FUNCTION.


Example

<?php

go(function()
{
    $result = Co\System::exec("md5sum " . __FILE__);
    var_dump($result);
});

Output:

<?php

array(3) {
  ["code"]=>
  int(0)
  ["signal"]=>
  int(0)
  ["output"]=>
  string(56) "2cbbb45d3dde31c1bad89a5fb60feb79  /root/swoole.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 August 31, 2022