WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a site running on server A and server B with a load balancer, shared database/etc, and a regular wp-cron task, how would I make sure that wp-cron only runs on server A, never on server B?

share|improve this question
Could you define a Constant, depending on which Server your wp-config.php is loaded? Afterwards it should be a piece of cake, making a conditional in the callback function of the wp-cron task. – fischi Feb 5 '13 at 11:29
That was my initial thought, but lets say this cron task is important, and it fires on server B, constant doesn't match, abort! Next time round, server B, same again, client notices that the 2 cron jobs for that day haven't fired, and raises a support issue. – Tom J Nowell Feb 5 '13 at 11:33
Yeah, got your problem. And got a solution :) – fischi Feb 5 '13 at 11:48

I would create a constant in the wp_config.php defining which Server you are on. So far so good, not really magic.

The tricky part, as you mentioned is to get the cron running for sure, and not aborting if it's accidentally the wrong server. Try the function like this:

function my_cron_callback() {
    if ( MY_SERVER_SETUP == "SERVER_A" ) {
    } else {
        set_transient( 'do_the_cron_on_server_a', 'true' );

Now, you have a transient set when the cron would run on the other server, but is not executed. The Function do_the_cron_action contains your script.

The last step is to hook a function to the init, checking if the server is ServerA and if the transient is set.

function check_if_a_should_execute() {
    if( MY_SERVER_SETUP == "SERVER_A" && get_transient( 'do_the_cron_on_server_a' == 'true' ) {
        delete_transient( 'do_the_cron_on_server_a' );
add_action( 'init', 'check_if_a_should_execute' );

That should do the trick.

share|improve this answer
Nice, the cronjob has a chance of being particularly heavy, perhaps adding to a later action rather than init might be better, one will test this afternoon! – Tom J Nowell Feb 5 '13 at 12:09
That depends on your preference and what the cronjob has to do. But I hope this method works for you :) – fischi Feb 5 '13 at 12:10
up vote 0 down vote accepted

I'm currently testing this in wp-config.php:

if ( php_uname('n') == 'wp4' ) {
    define('DISABLE_WP_CRON', true);

Where wp4 is the name of the second server in the setup

share|improve this answer

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.