public function lock()
    {
        if ($this->isRunning())
            return false;

        $pidFile = $this->directory.$this->filename;

        if (!is_writable($this->directory)) {
            throw new \Exception('Can not write to folder: '.$this->directory);
        }

        if (file_exists($pidFile) && !is_writable($pidFile)) {
            throw new \Exception('Can not write to file: '.$pidFile);
        }

        $file = fopen($pidFile, 'w');
        if ($file === false) {
            return false;
        }

        fputs($file, getmypid());
        fclose($file);

        return true;
    }