Quick and easy way
Probably the problem is that you are trying to enable wp-load.php from the wrong path. In the CLI environment, the path will not be the same as when you make an HTTP request to the file. Thus, you should fix your problem:
require(dirname(__FILE__) . '/../../../wp-config.php');
Right but Longer Way
Based on the comments of cale_b and in this article that he linked, there is a very correct way to do this Wordpress Cron task .
, , , my_cron_job(). script, . , 5 :
add_filter('cron_schedules', 'fively_interval');
function fively_interval($interval) {
$interval['fively'] = array('interval' => 5*60, 'display' => 'Once 5 minutes');
return $interval;
}
register_activation_hook(__FILE__, 'my_cron_job_activation');
add_action('my_cron_event', 'my_cron_job');
function my_cron_job_activation() {
wp_schedule_event(time(), 'fively', 'my_cron_event');
}
register_deactivation_hook( __FILE__, 'my_cron_job_deactivation' );
function my_cron_job_deactivation(){
wp_clear_scheduled_hook( 'my_cron_event' );
}
cron wp-cron.php 5 :
*/5 * * * * php-cli -f [path to your WP]/wp-cron.php
Update
, wp-cron.php cron, WP Cron ( cron -):
define('DISABLE_WP_CRON', true);
-, WP Cron, . 100%, , , wp_schedule_event cron, , . script, cron.
:
00:00:00:000 Server cron execute wp-cron.php
00:00:00:100 The job can be executed, so let it run
00:00:00:200 Wordpress finished to execute the job - it schedule the event in 5min
00:05:00:000 Server cron execute wp-cron.php
00:05:00:100 The job is planned for 00:05:00:200, no execution !
00:10:00:000 Server cron execute wp-cron.php
00:10:00:100 The job is executed
, ,, , . , . , , , wp_schedule_event - , 4min.
add_filter('cron_schedules', 'fourly_interval');
function fourly_interval($interval) {
$interval['fourly'] = array('interval' => 4*60, 'display' => 'Once 4 minutes');
return $interval;
}
, :
00:00:00:000 Server cron execute wp-cron.php
00:00:00:100 The job can be executed, so let it run
00:00:00:200 Wordpress finished to execute the job - it schedule the event in 4min
00:05:00:000 Server cron execute wp-cron.php
00:05:00:100 The job is planned for 00:04:00:200, so let it run!
00:10:00:000 Server cron execute wp-cron.php
00:00:00:200 Wordpress finished to execute the job - it schedule the event in 4min
00:10:00:100 The job is executed (planned for 00:09:00:200)
WP Cron .