Scalr\System\Zmq\Cron\PidFile::check PHP Method

check() public method

Checks pid
public check ( ) : integer | boolean
return integer | boolean Returns PID if it's found, or false otherwise
    public function check()
    {
        if (file_exists($this->file)) {
            if (!is_readable($this->file)) {
                $this->log("ERROR", "Could not open pid file '%s' for reading.", $this->file);
                exit;
            }
            if (!is_writable($this->file)) {
                $this->log("ERROR", "Could not open pid file '%s' for writing.", $this->file);
                exit;
            } else {
                //Reading from pid file
                $this->pid = intval(trim(file_get_contents($this->file)));
                //Trying to find that one
                $op = [];
                exec('ps -p ' . $this->pid . ' -o pid,command | egrep "' . preg_quote($this->command, '"') . '"', $op);
                $found = false;
                if (!empty($op)) {
                    foreach ($op as $str) {
                        $pid = substr(ltrim($str), 0, strpos(ltrim($str), ' '));
                        if ($this->pid == $pid) {
                            $found = $pid;
                            break;
                        }
                    }
                }
                if (!$found) {
                    //Trying to recover obsolete pid
                    $this->log("WARN", "Removing obsolete pid file '%s' as process %d does not exist", $this->file, $this->pid);
                    $this->remove();
                }
                return $found;
            }
        }
        return false;
    }