Seshan's Blog

keyboard_backspaceBack to Posts
WordPress Performance Tip: Use the System Cron!
June 8, 2019

One of the problems with PHP Web Apps is that they are only ever running for the duration of an HTTP Request. What I mean by that is that websites powered by WordPress, for example, aren’t continuously running on a host server. The only time WordPress gets to do any is when a user requests a page, and the webserver invokes PHP.

This is great, but sometimes WordPress needs to do other background tasks, such as internal tasks like random housekeeping, or tasks you scheduled like a scheduled post. The way WordPress handles this is through WP-Cron.

Basically, WP-Cron is a file (wp-cron.php) that is run by WordPress every time you load a page. When it’s invoked, it checks a list of scheduled tasks and sees if any need to be run. This works pretty well, but is also quite the performance issue. Since this happens when someone loads a page, it’s an extra thing WordPress is busy doing besides actually serving the page. Further, more time sensitive tasks, may not actually happen on time. For example, if a post is set to be released at 1:00PM, it may not actually make it out at exactly 1:00PM. It will be posted sometime after 1:00PM, whenever a request is made to the server.

So what is the solution? Use the system cron!

Unix-like systems such as GNU/Linux, macOS, BSD, etc, have a system called “cron” (which WP-Cron was named after). What we can do is use cron to periodically call wp-cron.php. This way, WordPress can do background tasks at regular intervals, and not clog up your users requests!


The first thing to do is disable running WP-Cron on every single request. Head over to your wp-config.php file and add the following somewhere near the bottom:

define('DISABLE_WP_CRON', true);

Once that’s done, it’s time to add a cronjob! A cronjob is basically a single line that describe when a command should be run. The WordPress developer page has a nice infographic of what a cronjob looks like:

plugin-wp-cron-cron-scheduling

For my needs, I want wp-cron.php to run every 15 minutes. So, add the following line to the bottom of the “nano /etc/crontab” file (atleast on Ubuntu) on your server (changing YOUR_SITE_URL to something like localhost, or your sites domain name). This technically doesn’t have to be on the same server as your WordPress site!

*/15 * * * * root wget --delete-after http://YOUR_SITE_URL/wp-cron.php

What this line does is use “wget” to call the wp-cron.php file on your server every 15 minutes. The “–delete-after” is to make sure wget doesn’t spam your host will random files.

All you need to do now is restart the Cron daemon to load the new changes:

# sudo systemctl restart cron

And that’s it! Now your WordPress site is setup to run background tasks at regular intervals and more efficiently! Enjoy!

Btw, if you’ve noticed that the there have been consecutive blog posts that have been posted at specific times: you can thank Cron!

ID: 111