Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Coroutine\System::exec(string $cmd): array
The Linux command you want to execution on the command line. A shell instruction.
Returns an array which contains information about the command during its execution. If the command failed to execution, then false
is returned.
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
.
<?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.