0

I made a PHP daemon launcher (I execute an independent [which is an argument passed to the daemon] script through exec()) 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 #!/usr/bin/php before the <?php 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.

Using:

  • 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)

Daemon's start() method:

/**
 * 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)
    {
        $this->setScript($script);
    }

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

    try
    {
        $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.');
        }

        usleep(50000);
        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;
}
19
  • It can't connect to mysql. Make sure that mysql is running on your server. Commented Sep 22, 2017 at 18:58
  • 1
    It might help if we can see your code (specifically your connection string) and that you tell us what you've already tried, since you state that you actually have searched and tried things. Then we won't need to repeat the same things here again. For example, have you checked this post? stackoverflow.com/questions/20723803/… Commented Sep 22, 2017 at 19:09
  • 1
    I get that. Things upstream effect everything downstream though. Environment apples and oranges... exec('whoami'); Don't call it that way or let's focus on what does not work in the given context. Then we can step back and think about 'why'. Commented Sep 22, 2017 at 19:26
  • 3
    Try this: before you run your exec command, try exec('which php'); and see if it gives the same as when you run it on the command line. I'm kind of curious if it's a permissions thing. Commented Sep 22, 2017 at 19:26
  • 1
    Try using the exact path for php in your exec command, then: $daemon_command = '/opt/local/bin/php '.$this->script... Commented Sep 22, 2017 at 19:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.