private function writePidfile()
    {
        if (isset($this->config['write_pidfile']) && $this->config['write_pidfile']) {
            if (!isset($this->config['pidfile'])) {
                throw new \Exception('Please supply a pidfile location.');
            }

            $this->pidfile = $this->config['pidfile'];

            if ($pidfile = fopen($this->pidfile, 'w')) {
                fwrite($pidfile, getmypid());
                fclose($pidfile);
            } else {
                throw new \Exception('Unable to open pidfile for writing.');
            }
        }
    }