Skip to content

Instantly share code, notes, and snippets.

@stefpe
Last active February 15, 2021 20:33
Show Gist options
  • Save stefpe/f86459640341d216a8caa34e45157bc5 to your computer and use it in GitHub Desktop.
Save stefpe/f86459640341d216a8caa34e45157bc5 to your computer and use it in GitHub Desktop.
async_process.php
<?php
class AsyncProcess
{
private int $pid;
private string $command;
/**
* AsyncProcess constructor.
* @param string $command
*/
public function __construct(string $command)
{
$this->command = $command;
}
public function exec(): void
{
$command = 'nohup ' . $this->command . ' > /dev/null 2>&1 & echo $!';
exec($command, $op);
$this->pid = (int)$op[0];
}
public function isFinished(): bool
{
exec('ps -p ' . $this->pid, $op);
if (!isset($op[1])) {
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment