Last active
February 15, 2021 20:33
-
-
Save stefpe/f86459640341d216a8caa34e45157bc5 to your computer and use it in GitHub Desktop.
async_process.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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