Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This will be a multi part question, with the aim of clarification and guidance.

I have a task which I require to be run every night at midnight. wp_schedule_event looks like its going to be my ticket, but I was unsatisfied with how WordPress handles cron jobs. From my understanding WordPress, on every page load will call wp-cron.php and then wp_cron.php checks to see if there are any cron jobs waiting to be executed.

While this is not really any issue, I am attempting to keep the amount of processies running on every page load to a minimum as I have already experienced a slowing of my site, and I want to avoid duplicates of a single cron job being run.

This code looks to accomplish the task I need to run every night:

register_activation_hook(__FILE__, 'cron_job_activation_hook');

function cron_job_activation_hook() {
wp_schedule_event( time(), 'daily', 'leaderBoardDailyUpdateHook');
}

add_action('leaderBoardDailyUpdateHook', 'update_leaderBoard_daily');

function update_leaderBoard_daily() {
//do my daily thing    
}

Then I found this which states I can disable the behavior of running wp-cron.php on every page load by using this snippet:

define('DISABLE_WP_CRON', true);

The article then explains how to manually call wp-cron.php using a standard php cron job, like so:

wget http://www.server.com/wp-cron.php > /dev/null 2>&1

But users were reporting that this will cause your server to create blank wp-cron.php files when it calls wp-cron.php on its scheduled run time.

A user suggests using:

wget -O - -q -t 1 http://www.server.com/wp-cron.php > /dev/null 2>&1

If you run the wget command as listed above, it will create a file named wp-cron.php.x each time it's run, where "x" gets incremented. You'll wind up with a lot files.

Better to use

wget -O - -q -t 1 .....

"-O -" logs all output to stdout, which is redirected to /dev/null "-q" makes it quiet "-t 1" tries only once. By default, wget will try 20 times on a failure.

Question 1:

Can I include the define('DISABLE_WP_CRON', true); in my activation hook or does it need to run on every page load?

Question 2:

Do the above functions and calls appear to be correct in setting up a cron job with wordpress?

Question 3:

Does the proposed solution for the file duplication appear to be correct?

Thanks for any input and clearance on this. Apologies for the short story.

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.