Skip to main content

WP Cron not run without a action from user (Web client, Browser... )

I read document ab WP cron and it say something like it require user perform some action, for example access homepage, browse some item etc. If no user enter site then cron job not run.
But there are many other information that confuse me. I just want to know does it run as scheduled as Native Linux cron or Windows Scheduler ?
Here are test script.
The basic idea is that let log timestamp of cronjob run to verify it run and in expected schedule.

Add these function to one of the cronjob (often placed in functions.php or lib folder in wp-content/themes/your-theme/...

add_filter( 'cron_schedules', 'add_every_min' );
function add_every_min( $schedules ) {
$schedules['every_min'] = array(
'interval'  => 60,
'display'   => __( 'Every Min', 'textdomain' )
);
return $schedules;
}

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'add_every_min' ) ) {
wp_schedule_event( time(), 'every_min', 'add_every_min' );
}

// Hook into that action that'll fire every three minutes
//add_action( 'add_every_min', 'test_wp_cron_1min' );
function test_wp_cron_1min() {
$test = date('Y-m-d H:i:s');
my_log($test);

return true;
}

function my_log($data) {
$log = WP_CONTENT_DIR . "/log2.txt";
 $ln = "\r\n";
 file_put_contents($log, json_encode($data).$ln, FILE_APPEND);
}

You should create a log file in wp-content/log2.txt and allow read+write permission to it.

To test, just run tail -f wp-content/log2.txt to watch log append.
When access, browse home page it run. If no action occurred => it not run so no log output.


Some useful WP CLI command:
List all cron event:
wp cron event list
wp cron schedule list # show schedule list (1 day, 1 hour, weekly ...)
wp cron event run test_1_min # execute cron job now (not have to wait)

wp cron event run --all # run all cron now
wp cron event run --due-now # I don't know what it mean/run

Comments

Popular posts from this blog

Rand mm 10

https://stackoverflow.com/questions/2447791/define-vs-const Oh const vs define, many time I got unexpected interview question. As this one, I do not know much or try to study this. My work flow, and I believe of many programmer is that search topic only when we have task or job to tackle. We ignore many 'basic', 'fundamental' documents, RTFM is boring. So I think it is a trade off between the two way of study language. And I think there are a bridge or balanced way to extract both advantage of two method. There are some huge issue with programmer like me that prevent we master some technique that take only little time if doing properly. For example, some Red Hat certificate program, lesson, course that I have learned during Collage gave our exceptional useful when it cover almost all topic while working with Linux. I remember it called something like RHEL (RedHat Enterprise Linux) Certificate... I think there are many tons of documents, guide n books about Linux bu

Martin Fowler - Software Architecture - Making Architecture matter

  https://martinfowler.com/architecture/ One can appreciate the point of this presentation when one's sense of code smell is trained, functional and utilized. Those controlling the budget as well as developer leads should understand the design stamina hypothesis, so that the appropriate focus and priority is given to internal quality - otherwise pay a high price soon. Andrew Farrell 8 months ago I love that he was able to give an important lesson on the “How?” of software architecture at the very end: delegate decisions to those with the time to focus on them. Very nice and straight-forward talk about the value of software architecture For me, architecture is the distribution of complexity in a system. And also, how subsystems communicate with each other. A battle between craftmanship and the economics and economics always win... https://hackernoon.com/applying-clean-architecture-on-web-application-with-modular-pattern-7b11f1b89011 1. Independent of Frameworks 2. Testable 3. Indepe