Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

WordPress crontrol pluging doesn't work?

Hello,

See attached. This is a crontrol I have set up (last item called php cron.

It was scheduled to run at 1:00 AM (GMT) last night, it didn't. I know because it writes the last time it runs into a database table.

If I run it "now" it works.

What's wrong?
crontrol7_2.jpg
Avatar of Peter Hart
Peter Hart
Flag of United Kingdom of Great Britain and Northern Ireland image

this article may shed some light on Wordpress cron jobs - apparently they only work if you get lots of visitors to the site.  i.e. whenever the website is active that's when it looks at the cron job and decides to run it. so if no one if looking at the site it doesn't fire

see
https://tommcfarlin.com/wordpress-cron-jobs/
I was wondering if that was the problem, no visits during the desired time.  PHP sites often have this problem because the only time the PHP code runs is when it is requested by a browser.  And if the code for the page doesn't run, the background process doesn't happen.
Avatar of Richard Korts

ASKER

Thanks for yor comments.

That is difficult to understand.

In a normal (non Wordpress) environment, you can schedule cron jobs to run at any time, using the UNIX / Linux cron capability.

Are you saying that if a word press site is not visited, cron does not run?
The problem is that it is not really 'cron', it is a PHP routine running under the Wordpress install.  Since your current host is not allowing access to the real 'cron' program, you're stuck with the Wordpress / PHP version.  Which does not run unless there is a page request.  Then the Wordpress administrative routines will run and run your 'crontrol' routine.

An alternative that has been proposed is that you 'call' the routine / page from a routine on a computer that will actually run cron or, on Windows, a 'scheduled task'.  That computer has to be running at the time you want your routine to run.
Dave,

Are you saying if there is a page hit on the site, the cron then runs, even if the time is past?

Seems totally illogical, but that would probably be ok.
I don't know about running if the time is far past.  Your original question implied that it didn't happen.  You probably need to write some kind of test program outside of Wordpress to find out.
ChilternPC and Dave are right in that "WordPress Crons" only run if the site has visits because the "cron" only fires when the WordPress instance runs.  It's not a real cron program.
"In a normal (non Wordpress) environment, you can schedule cron jobs to run at any time, using the UNIX / Linux cron capability."

The WordPress "cron" is a work around for people on shared hosting who cannot schedule a cron job due to hosting limitations. It is a sort of best effort thing, hoping to be better than nothing but it isn't offered as even equivalent to an operating system cron job.

You can schedule  cron jobs to run at any time, using the UNIX / Linux cron capability, even if there is a WordPress website in the equation. People often schedule a cron to "wget" a WordPress page to run the WordPress "cron", which is a bit of a workaround of a workaround.
Dave,

Regarding "An alternative that has been proposed is that you 'call' the routine / page from a routine on a computer that will actually run cron or, on Windows, a 'scheduled task'".

The host in this case is WPEngine, which is very restrictive.

So can I on another linux / unix host that I know allows cron set up a cron job that executes my program on WPEngine? For example, if my php program I want run on WPEngine is in the root of the site and is called do_cron.php, can I then use as the "program to execute" in cron on another host: "http://www.mysite.com/do_cron.php"?

That would be great but somehow I doubt that would work. Actually, I would probably have to pass a "password" in the query string & do a $_GET in the program to see if it's set to the value I expect, so that a hacker could not just execute that thing willy nilly.

Thanks,

Richard
SOLUTION
Avatar of William Nettmann
William Nettmann
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried it today. Worked great, except I set it to run at 3:00 PM central. I sent it to send me an email, it did at EXACTLY 1 PM Pacific. But the update time in the record I write to the database says 10:46 AM, Pacific.

?????
That means that the server time and your PHP 'default' time zone are out of sync.  Here is a test program I use to compare the default PHP/server time and the MySQL time.  This is important if you use the NOW() function in MySQL for the date and time.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP MySQL Time test</title>
</head>
<body>
<h1>PHP MySQL Time test</h1>
<?php 
//date_default_timezone_set('America/Chicago');
if (date_default_timezone_get()) {
    echo 'date_default_timezone_set: ' . date_default_timezone_get() . '<br />';
}

$Myoid = date("Ymd-His");
echo "PHP date and time: ".$Myoid."<br>";
// MySQLi Database Settings - Put your own in here
$dbuser = 'youruser';
$dbpass = 'yourpwd';
$dbname = 'yourdb';
$dbhost = 'localhost';
// Opens a connection to a mySQL server
$link = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($link->connect_errno) {
    echo "Failed to connect to MySQL: (" . $link->connect_errno . ") " . $link->connect_error;
}
// check session id value from previous page	
$sql = "SELECT NOW() as dnow";
if (!$res = $link->query($sql))  die('Error83: ' . $link->connect_errno);
$cart = $res->fetch_assoc();
$crows = $res->num_rows;
if (!isset($cart) || $crows < 1) {
	$cempty = "<pre>Shopping Cart is empty, please go back and try again.</pre>";
	//exit;
}
echo "MySQL date and time: ".trim($cart['dnow'])."<br>";

 ?>
</body>
</html>

Open in new window

I was wrong. It's the 4th of July. I wasn't paying close enough attention

The cron said this:

/usr/local/cpanel/bin/jailshell: http://watermanusa.staging.wpengine.com/load_rfqs.php: No such file or directory

That is the correct URL

So it DID NOT work.

Dave mentioned "curl". I've seen that but I don't know how that works. How does that work?
Well of course it said that.  I believe it needs a local, not a web path.  But in your situation I really don't know.  Were you able to run my test program above?
wget http://watermanusa.staging.wpengine.com/load_rfqs.php

Open in new window

Dave & William,

I changed the cron to use wget. It seems to work except for an odd thing about the time.

So I could test it soon, I set it to run at 10:00 AM Central US; the server is in Chicago. It ran & it sent me an email at EXACTLY 8:00 AM Pacific.

A while back, to determine if it was working, I put this code at the END of the program run by the cron:

$dt = date('Y-m-d h:i:s');
//echo "dt = " . $dt . "<br>";
$qryup = "UPDATE parms set last_rfq_load = '" . $dt . "'";
//echo "qry = " . $qryup . "<br>";
$rs = $wpdb->get_results($qryup);

(Note this uses the Wordpress $wpdb class to perform SQL operations)

I looked in he parms table; the time was set to 03:00:03.

I used a variation of Dave's time test program, it shows that the server with watermanusa.staging.wpengine.com is running on GMT, so that would be perfect. I had intended the use the 24 hr clock, I inadvertently used h instead of H for the hour; 3 PM GMT is 10 AM Chicago, so perfect.

EXCEPT, in the program load_rfqs.php, at the very top, I have this:

date_default_timezone_set("America/Los_Angeles");

I attached the variation of Dave's program I ran.

Then, just to test, I uncommented the first line (date_default_timezone_set) & ran it again. It showed the time in US Central.

So my ONLY concern is why mine doesn't show the time in US Pacific?

Seems odd.
date_time_test.php