Swoole Coroutine System: exec

Notice: the documentation is suitable for PHP extension swoole until v4.7.1, we only support PHP extension openswoole from v4.7.1

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.