Script Wont Run Correctly in Cron Job

Hi,

I have an issue thats driving me mad and i cant seem to fix the issue. I have a PHP script which i doesnt seem to run in from a Cron Job.

I’ve made a script which checks the Database to make sure a task is performed, if it hasnt performed and theres a number of rows left pending then it will load a URL to send me a sms message to inform me. Now i’ve got the PHP script running every 30mins via Cron, but it doesnt seem to be loading as i never get the text, the reason i know this is because iv edited the code so i get the message every time the script is loaded so i know its running fine. I’ve tried running the script in SSH and it works fine and i get the SMS message, but when i leave it for Cron to run the script every 30mins i dont get a message.


<?php
$con=mysqli_connect("REMOVED","REMOVED","REMOVED","REMOVED");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT *
FROM `BingoLeads`
WHERE `Grabbed` = 'NO'") or die(mysql_error());

header("Location: http://www.bulksms.co.uk:5567/eapi/submission/send_sms/2/2.0?username=removed&password=removed&message=Server+Error+Grabbing+Data&msisdn=removed");

if (mysqli_num_rows($result) > 0) {
echo "Software Stopped Working!";
//header("Location: http://www.bulksms.co.uk:5567/eapi/submission/send_sms/2/2.0?username=removed&password=removed&message=Server+Error+Grabbing+Data&msisdn=removed");

//echo '<iframe src="http://www.bulksms.co.uk:5567/eapi/submission/send_sms/2/2.0?username=removed&password=removed&message=Server+Error+Grabbing+Data&msisdn=removed"></iframe>';
} else {
	echo "error";
}
mysqli_close($con);
?> 


I’ve checked the firewall and i have opened port 5567, like i say when i run this via SSH and i type in:

lynx -source http://www.removed.co.uk/datacheck.php

Then it works fine and i get the SMS Message.

I have the following as my cron job:

0,30 * * * * /usr/bin/php -f /home/removed/public_html/datacheck.php

i’ve also tried the following as a cron job

0,30 * * * * lynx -source http://www.removed.co.uk/datacheck.php

But i dont get the SMS message. Its really annoying me and i cant seem to find out why its not running?

Is there a way i can check in real-time if the cron job has actually ran or when it was last ran?

Any help on this would be a big help for me.

Thanks

You’ll have to send the SMS using cURL.


header("Location: http://www.bulksms.co.uk:5567/eapi/submission/send_sms/2/2.0?username=removed&password=removed&message=Server+Error+Grabbing+Data&msisdn=removed");

curl to this instead of using a header redirect. The header function doesn’t work from a CLI call.

I’ve checked the firewall and i have opened port 5567, like i say when i run this via SSH and i type in:

lynx -source http://www.removed.co.uk/datacheck.php

And before you cite this, lynx is a command line browser. By invoking your script in this manner lynx makes a call to your server’s webserver software which in turn invokes the PHP just as if you’d visited that page. You might be able to get lynx to be invoked in a cron, but it would be cleaner to rewrite the PHP script to truly work from the command line - that way you don’t need to expose this script to a web path (and likely shouldn’t).

Is there a way i can check in real-time if the cron job has actually ran or when it was last ran?

I’d personally just write a log file. It’s as simple as


file_put_contents($logfile, $logmessage, FILE_APPEND);

Just remember to put line breaks on the tail of your message so you can read it easily.