isSupported = self::isSupported(); $this->pidFile = $pidDir . '/' . $pid . '.pid'; $this->timeCreation = time(); $this->markAsNotStarted(); } private function markAsNotStarted() { $content = $this->getPidFileContent(); if ($this->doesPidFileExist($content)) { return; } $this->writePidFileContent(''); } public function hasStarted($content = null) { if (is_null($content)) { $content = $this->getPidFileContent(); } if (!$this->doesPidFileExist($content)) { // process is finished, this means there was a start before return true; } if ('' === trim($content)) { // pid file is overwritten by startProcess() return false; } // process is probably running or pid file was not removed return true; } public function hasFinished() { $content = $this->getPidFileContent(); return !$this->doesPidFileExist($content); } public function getSecondsSinceCreation() { return time() - $this->timeCreation; } public function startProcess() { $this->writePidFileContent(getmypid()); } public function isRunning() { $content = $this->getPidFileContent(); if (!$this->doesPidFileExist($content)) { return false; } if ($this->isProcessStillRunning($content)) { return true; } if ($this->hasStarted($content)) { $this->finishProcess(); } return false; } public function finishProcess() { Filesystem::deleteFileIfExists($this->pidFile); } private function doesPidFileExist($content) { return false !== $content; } private function isProcessStillRunning($content) { if (!$this->isSupported) { return true; } $lockedPID = trim($content); $runningPIDs = explode("\n", trim( `ps -e | awk '{print $1}'` )); return !empty($lockedPID) && in_array($lockedPID, $runningPIDs); } private function getPidFileContent() { return @file_get_contents($this->pidFile); } private function writePidFileContent($content) { file_put_contents($this->pidFile, $content); } public static function isSupported() { if (SettingsServer::isWindows()) { return false; } if(self::shellExecFunctionIsDisabled()) { return false; } if (static::commandExists('ps') && self::returnsSuccessCode('ps') && self::commandExists('awk')) { return true; } return false; } private static function shellExecFunctionIsDisabled() { $command = 'shell_exec'; $disabled = explode(',', ini_get('disable_functions')); return in_array($command, $disabled); } private static function returnsSuccessCode($command) { $exec = $command . ' > /dev/null 2>&1 & echo $?'; $returnCode = shell_exec($exec); $returnCode = trim($returnCode); return 0 == (int) $returnCode; } private static function commandExists($command) { $result = shell_exec('which ' . escapeshellarg($command)); return !empty($result); } }