2018-05-07 10:51:08 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file is part of cocur/background-process.
|
|
|
|
*
|
|
|
|
* (c) 2013-2015 Florian Eckerstorfer
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Cocur\BackgroundProcess;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use RuntimeException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BackgroundProcess.
|
|
|
|
*
|
|
|
|
* Runs a process in the background.
|
|
|
|
*
|
|
|
|
* @author Florian Eckerstorfer <florian@eckerstorfer.co>
|
|
|
|
* @copyright 2013-2015 Florian Eckerstorfer
|
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License
|
|
|
|
* @link https://florian.ec/articles/running-background-processes-in-php/ Running background processes in PHP
|
|
|
|
*/
|
2018-07-26 08:33:50 -04:00
|
|
|
class BackgroundProcess {
|
|
|
|
|
|
|
|
const OS_WINDOWS = 1;
|
|
|
|
const OS_NIX = 2;
|
|
|
|
const OS_OTHER = 3;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $command;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $pid;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $serverOS;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $command The command to execute
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
public function __construct($command = null) {
|
|
|
|
$this->command = $command;
|
|
|
|
$this->serverOS = $this->getOS();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the command in a background process.
|
|
|
|
*
|
|
|
|
* @param string $outputFile File to write the output of the process to; defaults to /dev/null
|
|
|
|
* currently $outputFile has no effect when used in conjunction with a Windows server
|
|
|
|
* @param bool $append - set to true if output should be appended to $outputfile
|
|
|
|
*/
|
|
|
|
public function run($outputFile = '/dev/null', $append = false) {
|
|
|
|
if ($this->command === null) {
|
|
|
|
return;
|
2018-05-07 10:51:08 -04:00
|
|
|
}
|
|
|
|
|
2018-07-26 08:33:50 -04:00
|
|
|
switch ($this->getOS()) {
|
|
|
|
case self::OS_WINDOWS:
|
|
|
|
if (class_exists("COM")) {
|
|
|
|
$shell = new \COM("WScript.Shell");
|
|
|
|
$shell->CurrentDirectory = realpath(DOC_ROOT . "/exec");
|
|
|
|
$shell->run("cmd /C \"{$this->command}\"", 0, false);
|
2018-05-07 10:51:08 -04:00
|
|
|
}
|
2018-07-26 08:33:50 -04:00
|
|
|
else {
|
|
|
|
shell_exec(sprintf('"cd ' . realpath(DOC_ROOT . "/exec") . ' && %s %s %s"', $this->command, ($append ? '>>' : '>'), $outputFile));
|
2018-05-07 10:51:08 -04:00
|
|
|
}
|
2018-07-26 08:33:50 -04:00
|
|
|
break;
|
|
|
|
case self::OS_NIX:
|
|
|
|
$script = "cd " . realpath(DOC_ROOT . "/exec") . " && " .
|
|
|
|
sprintf('%s %s %s 2>&1 &', $this->command, ($append ? ">>" : ">"), $outputFile);
|
|
|
|
pclose(popen($script, "r"));
|
|
|
|
//$this->pid = (int) shell_exec("cd " . realpath(DOC_ROOT . "/exec") . " && " . sprintf('%s %s %s 2>&1 & echo $!', $this->command, ($append ? '>>' : '>'), $outputFile));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new RuntimeException(sprintf(
|
|
|
|
'Could not execute command "%s" because operating system "%s" is not supported by ' .
|
|
|
|
'Cocur\BackgroundProcess.', $this->command, PHP_OS
|
|
|
|
));
|
2018-05-07 10:51:08 -04:00
|
|
|
}
|
2018-07-26 08:33:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if the process is currently running.
|
|
|
|
*
|
|
|
|
* @return bool TRUE if the process is running, FALSE if not.
|
|
|
|
*/
|
|
|
|
public function isRunning() {
|
|
|
|
$this->checkSupportingOS('Cocur\BackgroundProcess can only check if a process is running on *nix-based ' .
|
|
|
|
'systems, such as Unix, Linux or Mac OS X. You are running "%s".');
|
|
|
|
|
|
|
|
try {
|
|
|
|
$result = shell_exec(sprintf('ps %d 2>&1', $this->pid));
|
|
|
|
if (count(preg_split("/\n/", $result)) > 2 && !preg_match('/ERROR: Process ID out of range/', $result)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-07 10:51:08 -04:00
|
|
|
}
|
2018-07-26 08:33:50 -04:00
|
|
|
catch (Exception $e) {
|
2018-05-07 10:51:08 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-26 08:33:50 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the process.
|
|
|
|
*
|
|
|
|
* @return bool `true` if the processes was stopped, `false` otherwise.
|
|
|
|
*/
|
|
|
|
public function stop() {
|
|
|
|
$this->checkSupportingOS('Cocur\BackgroundProcess can only stop a process on *nix-based systems, such as ' .
|
|
|
|
'Unix, Linux or Mac OS X. You are running "%s".');
|
|
|
|
|
|
|
|
try {
|
|
|
|
$result = shell_exec(sprintf('kill %d 2>&1', $this->pid));
|
|
|
|
if (!preg_match('/No such process/', $result)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-07 10:51:08 -04:00
|
|
|
}
|
2018-07-26 08:33:50 -04:00
|
|
|
catch (Exception $e) {
|
2018-05-07 10:51:08 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-26 08:33:50 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ID of the process.
|
|
|
|
*
|
|
|
|
* @return int The ID of the process
|
|
|
|
*/
|
|
|
|
public function getPid() {
|
|
|
|
$this->checkSupportingOS('Cocur\BackgroundProcess can only return the PID of a process on *nix-based systems, ' .
|
|
|
|
'such as Unix, Linux or Mac OS X. You are running "%s".');
|
|
|
|
|
|
|
|
return $this->pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the process id.
|
|
|
|
*
|
|
|
|
* @param $pid
|
|
|
|
*/
|
|
|
|
protected function setPid($pid) {
|
|
|
|
$this->pid = $pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function getOS() {
|
|
|
|
$os = strtoupper(PHP_OS);
|
|
|
|
|
|
|
|
if (substr($os, 0, 3) === 'WIN') {
|
|
|
|
return self::OS_WINDOWS;
|
|
|
|
}
|
|
|
|
else if ($os === 'LINUX' || $os === 'FREEBSD' || $os === 'DARWIN') {
|
|
|
|
return self::OS_NIX;
|
2018-05-07 10:51:08 -04:00
|
|
|
}
|
|
|
|
|
2018-07-26 08:33:50 -04:00
|
|
|
return self::OS_OTHER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $message Exception message if the OS is not supported
|
|
|
|
*
|
|
|
|
* @throws RuntimeException if the operating system is not supported by Cocur\BackgroundProcess
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
protected function checkSupportingOS($message) {
|
|
|
|
if ($this->getOS() !== self::OS_NIX) {
|
|
|
|
throw new RuntimeException(sprintf($message, PHP_OS));
|
2018-05-07 10:51:08 -04:00
|
|
|
}
|
2018-07-26 08:33:50 -04:00
|
|
|
}
|
2018-05-07 10:51:08 -04:00
|
|
|
|
2018-07-26 08:33:50 -04:00
|
|
|
/**
|
|
|
|
* @param int $pid PID of process to resume
|
|
|
|
*
|
2018-08-28 21:27:13 -04:00
|
|
|
* @return BackgroundProcess
|
2018-07-26 08:33:50 -04:00
|
|
|
*/
|
|
|
|
static public function createFromPID($pid) {
|
|
|
|
$process = new self();
|
|
|
|
$process->setPid($pid);
|
|
|
|
|
|
|
|
return $process;
|
|
|
|
}
|
2018-05-07 10:51:08 -04:00
|
|
|
|
|
|
|
}
|