CRON Issue - CRON runs but action not called? 😧

Hello everyone,

I have noticed that the cron section on our website is not working. I mean the crons are executing but the respective actions associated with it were not called. For this reason, our abandoned cart plugin was also not sending mails to the respective people.

I wrote a plugin to fetch reviews on a daily basis from a 3rd party website. A gist of the code is as follows:

register_activation_hook(__FILE__, 'my_plugin_activation');
register_deactivation_hook(__FILE__, 'my_plugin_deactivation');


function my_plugin_activation() {
    if (! wp_next_scheduled ( 'my_daily_fetch_review_cron' )) {
		wp_schedule_event(time(), 'daily', 'my_daily_fetch_review_cron');
    }
}

add_action('my_daily_fetch_review_cron', array('My_Review_Class','my_static_fetch_rating'));

Please ignore the function names or the nomenclature used. It is just a demo code to explain my point.

Now, the action should be called at the time the plugin is activated and then from that point onwards, on a daily basis.

This works perfectly on my local setup but does not run on the production server :frowning: I spent a lot of time to understand why this might be happening. Even disabled wordpress wp_cron and enabled server cron. From server cron, am pinging the wp-cron.php every 10 minutes.

Any guidance would be really appreciate. Any pointers to where I might look to debug this ?

Thank you in advance. You guys are awesome as usual :slight_smile:

Sumeet Chawla

Are you sure CRON are the problem ? If you do not receive email it’s maybe not due to CRON. Try to install a plugin like Easy SMTP to send your emails.

Thank you so much for the reply @virtubox

Nope, mail is not the issue as all other mails are going. am using Sparkpost API to send mails. I got to know about this issue while I was working on the feature I mentioned above. What I am doing is, fetching the review count from the 3rd party review site and updating the database to store it. So I created a wp_schedule_event for this to do this on a daily basis as the reviews get updated daily.

Now, when I activate the plugin, the wp_schedule_event is created and the action should be performed for the first time to update the value in the database. This happens properly on my local server but does not happen on the production one.

This made me realize that all wp_scheduled_events were actually not hitting it off even though they were invoked via the cron.

I have set DISABLE_WP_CRON as true and am pinging the wp-cron file via server cron every 10 minutes.

‘WP Crontrol’ is a really useful plugin for easily checking what wp-cron tasks Wordpress has scheduled (Tools/Cron Events), and for triggering them whilst testing.

What’s your crontab entry for this? And do you have it your user level crontab or roots crontab (they are different)…?

Running wp-cron via linux fixed your issue ?

Hey @marty thank you so much for the reply.

Yup, I have been trying to debug it using WP Crontrol too. It is indeed an amazing plugin. The scheduled task which I create, is listed there. I executed it again from WP Crontol but still the value did not get updated.

I think it is for the same reason that even Yoast SEO is creating multiple duplicate cron events for it’s usual tasks. There are like soo many of those. You can see on the top part of the image below :frowning:

On the local, when I execute it, it is not recreating the same event in the cron list. Where as on the production server, it is adding another entry as shown above?

I repeated the same steps on my local MAMP setup and it worked as expected. That is, deleted the DB value, executed the cron via WP-Crontol and the value was again filled by the method.

@virtubox These issues were happening by default. So I tried to disable wp-cron and enable the linux version of it (cause anyway that is better for performance). But that hasn’t solved the issue either :frowning:

I previously had W3TC and thought it might be causing issues. So shifted to Redis cache system (which was due for quite sometime and it is awesome :smiley: ). But still no solution. I am sure I am missing something somewhere :see_no_evil:

@marty I have followed this guide to create the linux cron https://easyengine.io/tutorials/wordpress/wp-cron-crontab/

But the cron thing will come into picture when the events are working but the schedule is getting missed. At present, the schedules were working (when I had the default php wp-cron active) but the events were not getting called.

Try to switch to --wp, without any cache :

ee site update yoursite.tld --wp

This way, you will see if the issue come from the cache

Great, I will take a backup and try this out and update here. Thank you!

Can’t I just disable the redis cache at the moment, disable object cache and purge all the existing cache to test it out? Oh, but the nginx full page caching configuration would still be there. Good point, better to change it to without cache and then try it out.

Easyengine change the configuration of nginx for each cache solution, that’s why it’s better to use ee site update, but don’t worry about your website data, it will only remove the plugin and set back the proper configuration. Have you switch from w3tc like that or manually ?

Oh awesome! Yes, I moved from W3TC using the update command itself. It automatically removed the old W3TC plugin, installed and configured the Redis Object cache plugin too.

Hey guys,

So I removed the redis cache mechanism and shifted to normal --wp version. Now when I went to Wp-Crontol page after that, all my previous wp_seo events got removed and the list looked clean like it should. I tried executing the cron via the “run now” command and it worked.

After that, I shifted back to the --wpredis cache system, activated the object cache. I deleted the db value (the reviews I was fetching) and re-tried the “run now” command, it did not work :frowning:

Initially, in the next run column, it was showing (2 minutes) etc. But after the time passed, it got stuck on the (now) message. Whenever I go to the cron-tab page, it just shows next run as (now) even though the time listed is that of the last execution, which never worked as expected. This happens for all the listed events i.e. once the next run time crosses, it only shows (now).

So to dig deeper, I tried executing the cron using wp-cli. When I executed the command to run all due events, it just infinitely started excuting all the wpseo_ events which were previously created. I had to hit ctrl+c to stop execution.

The next thing I checked for was all the events which are suppose to run next in the cron list using the command

wp cron event list --fields=hook,next_run

I got sooo many entries of the same wpseo events, even dating back to the year 2016. :open_mouth:

I temporarily disabled the Yoast SEO plugin, ran a delete event command wp cron event delete wpseo_ping_search_engines and a total of 574 instances of the cron events got deleted.

Did the same thing for the wpseo_hit_sitemap_index event as well.

After that, I again checked the list of events in the ‘next run’ list and did an execute due all for all events listed.

I checked the database and this time the value was there!

So for some reason, my whole cron system is kinda messed up and I am unable to figure out what might be the issue. :crying_cat_face:

This is the crontab entry which I have

*/10 * * * * cd /var/www/mysite.tld/htdocs; php /var/www/mysite.tld/htdocs/wp-cron.php?doing_wp_cron > /dev/null 2>&1

Is there any way of verifying if the above version is actually executing all the cron items at all? good thing I am not using the wp cli version as it would have just stuck.

Update: When I run this on the command line, I get an error of “Could not open input file” but if I give the command without ?doing_wp_cron no error comes. Am I missing something here.

UPDATE:

It seems like the cron command was not working

*/10 * * * * cd /var/www/example.com/htdocs; php /var/www/example.com/htdocs/wp-cron.php?doing_wp_cron > /dev/null 2>&1

The ?doing_wp_cron was giving error when I executed the script using the php command. It was working fine without it. So I removed it in the crontab -e file.

It worked!! :slight_smile:

I had an abandoned cart plugin installed which was dependent on the cron to send reminder mails. As soon as the cron started working, it sent out 1400 emails :disappointed: :see_no_evil:

UPDATE 2

After last night, all the event’s next run relative time column again became “now”.

I did ee clean --redis and then it is showing the proper next run relative timing.

So definitely something related to the object cache. Any tips?

Hello @sumeetchawla, you may have to exclude some groups in the redis-object-cache rules. Go in Settings > Redis and use Show diagnostic to see if there are extra group you should exclude. Then define them with the variable

WP_REDIS_IGNORED_GROUPS (default: ['counts', 'plugins']);