Adrián Adrián - 5 months ago 48
MySQL Question

SQLSTATE[HY000] [2002] No such file or directory - PHP Daemon

I made a PHP daemon launcher (I execute an independent [which is an argument passed to the daemon] script through

) and the script that this PHP daemon runs uses a PDO wrapper that I made as well.

The thing is, when I run the script through the PHP daemon (
$ php
), my PDO wrapper can't connect and throws
SQLSTATE[HY000] [2002] No such file or directory

The daemon_script.php includes
before the
opening tag.

Now, I've been searching here since yesterday and found a couple of approaches but none is my specific case and can't manage to make it work, so I thought you'd have an idea on what I'm doing wrong.

Thanks in advance.


  • PHP 7.0.21 (although intended to implement it with PHP 5)

  • MYSQL Ver 14.14 Distrib 5.6.34, for osx10.12 (x86_64)


* Starts the script.
* Also receives a script and sets it, then it runs it.
* @param string $script Optional script to start.
* @throws Exception Could not init.
* @throws Exception Could not save pid.
* @return boolean
public function start($script = '')
if ($script)

$initialized = false;
$daemon_command = 'php '.$this->script.' > script.log 2>&1 & echo $! &';

$daemon_pid = exec($daemon_command, $output);

if (!$daemon_pid)
throw new Exception('Could not initialize. Invalid script: '.$this->script);
if (!file_put_contents($this->pid, $daemon_pid))
exec('kill '.$daemon_pid);
throw new Exception('Could not save process id "'.$daemon_pid.'"… killing it.');

if (!($initialized = $this->checkPID($daemon_pid)))
file_put_contents($this->pid, null);
throw new Exception('Script died unexpectedly!');
catch (Exception $e)
$this->errors = array(
'code' => $e->getCode(),
'message' => $e->getMessage()
) + $this->_errors;

return $initialized;

Answer Source

You need to set the full path to PHP in your command in order for exec() to properly find it. Since php is located in /opt/local/bin/php, just add it like this:

$daemon_command = '/opt/local/bin/php '.$this->script.' > script.log 2>&1 & echo $! &';

As a note, cron often works the same way, because it doesn't access the same PATH variables that a command-line user does.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download