Triggering Wordress cron jobs function on server side - wordpress

Suppose I have a Wordpresss cron job scheduler function ( which runs only on page loads ) on server as follows :
add_action('init','create_single_schedule');
add_action('single_cron_job','single_cron_function');
function single_cron_function(){
Do cron here
}
function create_single_schedule(){
//check if event scheduled before
if(!wp_next_scheduled('single_cron_job'))
//schedule event to run after 60 seconds (or one minute)
wp_schedule_single_event (time()+60, 'single_cron_job');
}
Now the problem is if no one opens my website , how can i call create_single_schedule() to schedule/trigger a cron job ?

You have to set up a real cron job on your server to call wp-cron.php at your desired interval.
First, disable WordPress cron on wp-config.php:
define('DISABLE_WP_CRON', true);
If you have cPanel, set up a cron job to run every 5 minutes, or every hour and to execute the command:
wget http://www.example.com/wp-cron.php
More info:
Hooking WP-Cron Into the System Task Scheduler | Plugin Developer Handbook | WordPress Developer Resources
Should I disable WP_CRON and instead trigger wp-cron.php from server every few mins?
Insights into WP-Cron: An Introduction to Scheduling Tasks in WordPress

Related

how to stop action scheduler 3.0 in wordpress 6.0?

I want to stop all my action scheduler when I am login in my wp-admin, instead of it I want to manage all cron should be handle by cron background of my server. is it possible please tell me the way I already spend lot of time to stop it from wp-admin ajax process.
Currently its working as below
The async queue runner initiates action processing when an admin user logs into the WordPress administration dashboard. It also uses loopback requests to process multiple batches of actions in a sequence of requests so that instead of processing actions in just one request, once a queue starts processing, it will continue to process actions in a new request until there are no actions to process, or loopback limits are reached.
So I just want to break this flow and instead of this I want to set my cron that can handle all the stuff so I don't need to login in my admin every time to run the all scheduler.
Add this in your functions.php to disable Action Scheduler:
<?php
function just_disable_default_runner() {
if ( class_exists( 'ActionScheduler' ) ) {
remove_action( 'action_scheduler_run_queue', array( ActionScheduler::runner(), 'run' ) );
}
}

Understanding WordPress Cron

What will be the best way to activate WordPress cron if your site has no hits or visits?
Just starting out my new DAILY BIBLE QUOTE blog and I have no hits yet. I have scheduled a post to published “once every day at 6:00 AM”. Since I have no hits I’m afraid the scheduled post will not get published, so i add cronjob on my shared cpanel hosting using the code below:
0 6 * * * wget -O /dev/null --timeout=120 http://example.com/wp-cron.php?doing_wp_cron=true
So my question is,
Will the scheduled post gets published using the command above or querying only the site http://example.com/ on the cron command is enough to do the job?
Do i need to scheduled the post at 6:05AM and add crontab to run at 6:00AM?
Do not run cronjob on the site hosting since the IP is going to be same, so better use a cron service like EasyCron using the FREE plan https://www.easycron.com/user/plan since the IP is going to be different so that my site will think of it as a new visit, hence activating wp-cron.
To address your questions:
Yes, you would simply need to load the homepage of your site (though you could load any page of the site) to trigger the WP Cron event. When you load the WordPress stack, on any page, it will load the entire WordPress stack and part of that loading is checking the database to see if any cron events are ready to run. The cron information is saved in the database with information on the time the next instance of that cron runs and the function/hook to run at that time, if anything matches it will fire off the event.
For more information on WordPress cron, you can review their documentation here:
https://developer.wordpress.org/plugins/cron/
Why not schedule the post at 5:59AM and then run the cron at 6AM? That would ensure that it gets published right on the minute.

How can I disable the git fileserver update schedule

I've set up a hook on my gitlab server to call salt-run fileserver.update from a post-update hook.
How can I disable the schedule that does a update every 60 to reduce the load on my gitlab server?
The 60 seconds interval in which the Git filesystem is updated is defined by the loop_interval setting, which you can set in your master configuration file:
# The loop_interval option controls the seconds for the master's maintenance
# process check cycle. This process updates file server backends, cleans the
# job cache and executes the scheduler.
#loop_interval: 60
However, this interval controls not only the GitFS update schedule, but also a number of other maintenance tasks, so you should not increase this interval by too much.
From a quick reading of the source code (I'm not a core Salt developer though, so I might be mistaken), the GitFS update is hard-coded to run on the same schedule as these other maintenance tasks. There does not appear to be a way to disable or change the interval of only the GitFS update schedule.

Wordpress Multi-Master DB Replication: Deadlock when updating cron table in wp_options

We're running Wordpress in an environment that features a multi-master DB behind a load-balancer. The error log was filling up with a deadlock error when WP tried to update the cron table in wp_options. We disabled wp-cron altogether but are still seeing the error, so, two questions:
1) What causes the cron table in wp_options to be updated?
2) It appears to run on every page load. Can this be disabled and a cronjob setup to run it periodically in crontab?
Thanks
Wordpress uses wp-cron.php as a means for running scheduled tasks when the user doesn't have access to or want to setup cronjobs via Unix. This process looks at the scheduled jobs in the cron table in wp_options and if the specified time (or more) has elapsed then the job executes.
wp-cron.php uses wp-includes/cron.php (the Wordpress Cron API) to run scheduled jobs. In cron.php you'll find a number of functions that update the cron table table, these functions are all around the scheduling of events.
Any function of Wordpress or plugin that requires a scheduled event uses the Cron API to do so. However, the action of scheduling an event (even if it already exists) updates the cron table in wp_options. Even with wp-cron.php totally disabled, these elements of Wordpress/the plugin are loading and scheduling their events, trying to update the cron table in the process.
I've not figured-out exactly why the deadlock occurs, other than knowing it must be related to the DB/site config, but I do now know that Wordpress is behaving itself.
I've run into this same issue -- the databases would go out of sync very quickly. Certain plugins made it occur faster (they scheduled lots of cron jobs), but even with them disabled, eventually the errors would block replication.
I was able to keep replication working by doing two things.
The first, in my.ini, was to add:
slave-skip-errors = 1062
This instructs MySql to skip creating entries when a duplicate key already exists. My cluster is set up as active-passive, so in theory, there should be no "real" writes to the passive MySql node unless the active node is down, in which case, there will be no "real" writes to that node. The only stuff that gets written to the passive node is as a result of wp-cron jobs, which (in theory) are also running on the active node.
The second, in each site's wp-config.ini, was to add:
/** disable cron */
define('DISABLE_WP_CRON', true);
This blocks wp-cron from running at all, so either one of these solutions should work on their own.
Another option would be to disable wp-cron, but leave the full database syncing in place, and schedule a script to call each site's wp-cron.php (you'd be accomplishing manually what the wp-cron service does automatically). That way, it will only run on the active node and the data should be synced over to the passive node with no problems.

How can I automatically run cron without pauses?

I need to index 80.000 nodes.
The max amount of nodes I can index per each cron run is 500.
I need to run crone 80.000 / 500 times to index the entire website.
How can I automatically schedule these runs (when a run is finished, the next run automatically should start)?
I don't have SSH access so I cannot use drush.
Thanks
All cron does is visit yoursite.com/cron.php
So you could use cron/schedule task/etc on a local machine.
Did you try Poormanscron?
A module which runs the Drupal cron operation using normal browser/page requests instead of having to set up a crontab to request the cron.php script. The module inserts a small amount of JavaScript on each page of your site that when a certain amount of time has passed since the last cron run, calls an AJAX request to run the cron tasks. Your users should not notice any kind of delay or disruption when viewing your site. However, this approach requires that your site gets regular traffic/visitors in order to trigger the cron request.
Why don't you set a cronjob every 4 minutes or so? Just make sure that the interval between cronjobs is longer than the time it takes to run the cron script, so it won't overlap.
Give a try to Apache Solr Search module in drupal.
To reiterate and clarify other answers: As long as you haven't explicitly blocked it in .htaccess or Apache configuration, you can trigger Drupal's cron.php simply by visiting yoursite.com/cron.php from any browser. You can also set up your local machine (or any other machine that has web access, really) to run its own cronjob which triggers your site's cron.php. This process varies from platform to platform, but for example, on most Linux systems, you could run crontab -e and add a line like this:
0 * * * * wget -O - -q -t 1 http://www.example.com/cron.php
# Run example.com's cron tasks at the beginning of every hour.
or possibly:
*/5 * * * * wget -O - -q -t 1 http://www.example.com/cron.php
# Run example.com's cron tasks at every five minute interval.

Resources