How to Replace WordPress Cron With A Real Cron Job

wpcron-missed-scheduleWe are all too familiar with the “Missed Schedule” status, aren’t we? I am sure it is very frustrating to see that all your scheduled posts went MIA.

WordPress comes with its own cron job that allows you to schedule your posts and events. However, in many situations, the WP-Cron is not working well and leads to posts missed their publication schedule and/or scheduled events not executed.

To understand why this happen, we need to know that the WP-Cron is not a real cron job. It is in fact a virtual cron that only works when a page is loaded. In short, when a page is requested on the frontend/backend, WordPress will first load WP-Cron, follow by the necessary page to display to your reader. The loaded WP-Cron will then check the database to see if there is any thing that needs to be done.

Reasons for WP-Cron to fail could be due to:

  • DNS issue in the server.
  • Plugins conflict
  • Heavy load in the server which results in WP-Cron not executed fully
  • WordPress bug
  • Using of cache plugins that prevent the WP-Cron from loading
  • And many other reasons

There are many ways to solve the WP-Cron issue, but the one that I am going to propose here is to disable the virtual WP-Cron and use a real cron job instead.

Why use a real cron job?

By using a real cron job, you can be sure that all your scheduled items are executed. For popular blogs with high traffic, using a real cron job can also reduce the server bandwidth and reduce the chances of your server crashing, especially when you are experiencing Digg/Slashdot effect.

Scheduling a real cron job

To configure a real cron job, you will need access to your cPanel or Admin panel (we will be using cPanel in this tutorial).

1. Log into your cPanel.

2. Scroll down the list of applications until you see the “cron jobs” link. Click on it.

wpcron-cpanel

3. Under the Add New Cron Job section, choose the interval that you want it to run the cron job. I have set it to run every 15minutes, but you can change it according to your liking.

wpcron-add-new-cron-job

4. In the Command field, enter the following:

wget -q -O - http://yourwebsite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

Replace http://yourwebsite.com with your website URL.

5. Click the “Add New Cron Job” button. You should now see a message like this:

wpcron-current-cron-job

6. Next, using a FTP program, connect to your server and download the wp-config.php file.

7. Open the wp-config.php file with a text editor and paste the following line:

define('DISABLE_WP_CRON', true);

8. Save and upload (and replace) this file back to the server. This will disable WordPress internal cron job.

That’s it.

To test it out, it is best to schedule a post 5 – 10 minutes after you have completed the above configuration. Wait and see if the WordPress internal cron job is fired. If yes, then congratulation, you have successfully scheduled a real cron job for WordPress. If no, go through the steps above and see if you have made any mistake. Alternatively, contact your web host and get them to configure for you.

  • http://twitter.com/glennbennett Glenn Bennett

    Thanks a lot for this. It took me way to much time to find this info.

  • crepmaster

    i’m a little bit confuse with cron, cron-wp etc… i need to update a table one a day. and i built the function level_update. Where do i need to insert that function? in wp-cron.php?

  • http://maketecheasier.com/ Damien Oh

    the functions.php file in your theme.

  • Anupam

    Thanks a lot. Your post was really helpful.

  • http://www.juliabarnickle.com/ Julia Barnickle

    Thanks Damien – it worked for me.

  • XcKoN

    Cool, this was the explanation that i was looking for =).
    Just a question, what would happen if the cron takes 30 minutes or one hour to finish, because you are running all the crons every 15 minutes, WP keeps a track of what crons are running or something like that?

  • http://maketecheasier.com/ Damien Oh

    You only set a cron to run WP cron, so that is not an issue if the cron the 30 minutes to complete. WP will do the work.

  • http://twitter.com/lanegoldberg Lane Goldberg

    So I have a plugin that relies on wp-cron. When certain things are updated in the backend it runs WP Cron. For that reason, I don’t want to define(‘DISABLE_WP_CRON’, true); Do you think there would be any problems if I run crontab AND keep WP_CRON enabled?

  • http://maketecheasier.com/ Damien Oh

    Nope. The real cron job is calling the wp-cron as well, so there are no conflicts when both are enabled.

  • Tevya Washburn – FS

    Will this work for Multisite? Seems like I saw somewhere that wp-cron had to be called for each site on the network when using Multisite.

  • http://maketecheasier.com/ Damien Oh

    This won’t work in a multisite setup. You will have to setup a cron job for each domain mapped in the multisite settings.

  • http://twitter.com/gresg2xyro Greg Rosenberg

    Am I not mistaken, but you want to “fire” the wp cron job in the wp-cron.php not the wp-config.php?

  • http://maketecheasier.com/ Damien Oh

    wp-config.php does not do the cron job. The file that does the cron job is the wp-cron.php. So it is logical to “fire” the wp-cron than then “wp-config”.

  • http://twitter.com/gresg2xyro Greg Rosenberg

    I was referring to your own instructions above:

    7. Open the wp-config.php file with a text editor and paste the following line:

  • http://maketecheasier.com/ Damien Oh

    I understand what you mean now. The wp-config.php is the file that contain all the configuration options. Since we want to disable the wp cron, wp-config is definitely the place to go. However when executing the actual cton job, the file to be called us wp-cron.

    Hope this is clear.

  • http://www.midbeam.com/ Rohit Agarwal

    Thanks Buddy!! I had some problem with wordpress cron job.. now I can easily manage cron job. :-)

  • Vivianne

    Hi Damien! I bought a plug in called Follow Up Emails to work in conjunction with Woo Commerce and I have it set to send a follow up email 2 minutes after a product order is processing. It is not working! These are actual orders so I’m not sure it fits into the category you describe above. The Follow Up Emaill seems to work ok when I ask it to send 2 minutes after the order is completed but I have to manually check it as completed and it is not convenient for customers to wait for me to check the completed box to get their downloads, etc. Do you think going through the process you describe would help to fix my situation? Thanks in advance! Are you available for hire to troubleshoot??

  • http://maketecheasier.com/ Damien Oh

    There are few possibilities: The followup email is not hooking to the correct action, that why it is not firing off after the order is completed. Alternatively, the followup email could be working fine, but the cron job is not firing. I think you will need to contact the developer and have him/her to look into the issue. If you need my help, feel free to contact me at twitter @damienoh.

  • http://www.liewcf.com/ LiewCF

    Thanks for the detailed instruction. This is very useful. :)

  • http://www.facebook.com/profile.php?id=100000816994899 Maruf Alam

    Helpful post. Thanks…

  • littleguy

    What will the behaviour be if you setup the Cron job but don’t specify DISABLE_WP_CRON. For example if Cron job stops working I would still like WP to continue doing “lazy mans” (at page load) cron tasks. So can you have the cake and eat it too?

  • http://maketecheasier.com/ Damien Oh

    Nope. It doesn’t matter because they are all calling the same function. On a side note, if you are running a heavy cron job in the backend, calling it frequently (by not enabling DISABLE_WP_CRON) might add burden to your server.

  • http://www.emagrecercalorias.com.br/ Mariana Silveira

    Hello.

    I had to add -O /dev/null to my wget command. Otherwise my home dir got full os zero sized wp-cron.php.xxx files.

  • http://twitter.com/mainelementlove Tyrika C Williams

    Vivianne, did you resolve this issue? If so would you mind sharing what the solution was? I am having the exact same issue. Thanks in advance!

  • Sander Bol

    Hi Damien – could you explain why we need to use wget to trigger the cron (ie: making a roundtrip through the network interface and the webserver), instead of running the PHP script directly on the host?

  • http://twitter.com/Danny_Bookworm Danny Bookworm

    Thanks for your great tutorial! Now, the WordPress Plugin for my Newsletter askes me for a “secret hashtag”. Do you have any idea where I can find this or what I have to do?

  • http://twitter.com/jason_coleman Jason Coleman

    I think by default WordPress will run unpredictably when called from the command line. For example, it expects to see a $_SERVER['REQUEST_URI'] and other globals when run. I’m sure you could rig something up to execute the crons through the command line, but it wouldn’t be as supported as this wget method is on most hosting setups.

  • http://www.zoomve.com/ Rohit Agarwal

    Its very helpful for me!!

  • WP Don

    Will this work for subdirectories vs. multisited domains?

  • Anni

    For users whose cloud server doesn’t have Cron, third party cron service like easycron.com is a good alternative. It’s quite reliable with log and email notification feature.

  • Andi G.

    Thanks so much for this. I just followed your instructions this morning, and I FINALLY had a scheduled post actually work! This was hugely helpful!

  • http://www.adeptecommerce.com/ Alex Montoya

    Where in the PHP file do we paste define(‘DISABLE_WP_CRON’, true);

    ???

  • http://maketecheasier.com/ Damien Oh

    In the wp-config.php file. It should be in the root folder (or a level above the root folder).

  • Tier II Tech Support

    I used this method to set my multiple sites up to run staggered through the night.
    So far it’s been working!
    http://stickystatic.com/tech/disable-wordpress-cron-jobs/