Database connection error from PHP script launched by a cron job

Status
Not open for further replies.

osusigna

New Member
Messages
9
Reaction score
0
Points
1
Hi there,

I wrote a PHP script that I would like to launch everyday with a cron job. It should connect to my database with PDO and get some information from it, but what I get is an error (more details below) even though the same code works well when launched from a browser.

Here's my cron job:
Code:
0 0 * * * /usr/bin/php -q /home/osusigna/www/delete.php >> /home/osusigna/etc/deletelog.txt 2>&1
Here's the beginning of my delete.php script:
Code:
// I heard absolute paths would be better than relative paths for a cron job, but no luck
if ($_SERVER['HTTP_HOST'] == 'localhost')                        $wwwPath = './';
else if ($_SERVER['HTTP_HOST'] == 'osusignature.x10.mx')         $wwwPath = '/home/osusigna/www/';

// This file defines variables about database connection: DB name, user and password.
include($wwwPath.'resources/globalVariables.php');

// Create connection to database
try {
    $bdd = new PDO('mysql:host=localhost;dbname='.$dbName, $dbUser, $dbPassword);
}
catch(Exception $e) {
    die('Error: '.$e->getMessage());
}

[...]

And this is the error I get in etc/deletelog.txt:
Code:
Error: SQLSTATE[28000] [1045] Access denied for user 'osusigna'@'localhost' (using password: NO)
As I said, the script works well when launched from a browser but not from a cron job. Am I missing something?
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
It is not setting your DB username (it should be osusingna_SOMETHING) or your password (note it is saying that you are not using a password).
 

osusigna

New Member
Messages
9
Reaction score
0
Points
1
Yes it is. Those are defined in my globalVariables.php that I include at the beginning of the script just before the database connection. And yeah they are correct and well formed, otherwise it wouldn't work with the web browser. The "using password: NO" is just bizarre.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
In that file, your database user is set to 'osusigna' ?
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Error: SQLSTATE[28000] [1045] Access denied for user 'osusigna'@'localhost' (using password: NO)

So, it seems that the include is messing up somehow.
 

osusigna

New Member
Messages
9
Reaction score
0
Points
1
Eh, you were right. I replaced the variables with raw data and it worked. I am puzzled :confused:
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
try using...

include('/home/osusigna/public_html/resources/globalVariables.php');
 

osusigna

New Member
Messages
9
Reaction score
0
Points
1
Ok, I just understood why it doesn't work.

In my globalVariables.php file, some variables are created and initialized depending on the status of $_SERVER['HTTP_HOST'] (because I have three servers to manage: x10, another one and my personal localhost). I just launched a cron job running a temporary PHP file that writes to a file the content of $_SERVER['HTTP_HOST'], and it seems that this is empty. As a consequence, my database variables were just uninitalized and the DB connection failed.

Alright, I'll try to find another way to bypass this issue. Thanks for your support! :)
 
Status
Not open for further replies.
Top