Swapping WP-Cron for a real server cron job

WordPress has a scheduler of its own, WP-Cron, which handles scheduled posts, update checks, emptying the bin and the e-mails your plugins send. The name misleads: it is not a cron job. It is a file that only runs when somebody visits the site.

That causes two opposite problems, and one of them is yours. What to do in general is in cron jobs; here is the WordPress side, step by step.

Your site What happens
Few visits a day Tasks sit waiting. The post scheduled for 9 in the morning goes out at 3 in the afternoon, when somebody arrives. Then they all fire at once, on top of that visitor, who is left staring at a stalled page.
Many visits a day The opposite: the scheduler is consulted far too often, and each consultation boots the whole of WordPress. It is repeated work competing with real visitors.

The swap, in two steps that do not come apart

Turning it off without creating the task is worse than not touching it. Add the line to wp-config.php and forget the rest, and scheduled posts stop going out, shop e-mails stop being sent, and nothing throws an error. Do both in the same sitting, or neither.
1 Switch off the internal scheduler. In wp-config.php, above the line telling you to stop editing, add define( 'DISABLE_WP_CRON', true );.
2 Create the task in cPanel. Under Advanced, open Cron Jobs. If you cannot see the icon, type cron into the panel’s search box.
3 Pick the frequency. Every five minutes is plenty for practically everything. In the common-frequency list that is */5 * * * *.
4 Write the command, choosing one of the two forms in the table below.
5 Put your address in the notification field, at the top of the page, for the first few days. You want whatever the task prints, so you know it runs.

The two ways to write the command

Form The command and when to use it
Straight through PHP /usr/local/bin/php -q /home/ACCOUNT/public_html/wp-cron.php. Swap ACCOUNT for your account name, and the path for the site folder. It is the fastest form and it never touches the web. /usr/local/bin/php uses the PHP version chosen for your account.
Over the web, with curl /usr/bin/curl -s "https://asuaempresa.co.mz/wp-cron.php?doing_wp_cron" >/dev/null 2>&1. It is the form plugins usually document, and the only one that works when the site is not on the account where you create the task.
If the application hands you a ready-made line, use theirs. It always beats ours: whoever wrote the plugin knows what it needs.
If the site is password-protected, the curl form will not do. With Directory Privacy on wp-admin, or with the whole site protected, the request is refused and the task never runs. Use the PHP form, which does not go through the web server.

Confirming it works

This part is not optional. A silent task that never worked looks exactly like one that does.

1 Schedule a post for ten minutes from now and check it went out on time, without your having opened the site.
2 Install a WordPress task-management plugin. It lists the scheduled tasks, the time each one runs and which ones are overdue. It is the clearest way to see that the new cron is feeding WordPress.
3 For the first few days, read the e-mail the task sends. Once you trust it, silence it by adding >/dev/null 2>&1 to the end of the command.

Mistakes that keep coming back

What you did Why it does not work
Scheduled it every minute That is 1440 starts a day competing with visitors, and if one round takes longer than a minute they stack on top of each other. Why that weighs is in admin-ajax eating your resources and in cron jobs.
A relative path in the command Cron does not start in the site folder. Paths have to be complete, from /home/ onwards.
A different PHP version If you point straight at one version’s binary while the site runs on another, extensions are missing and the task fails with odd errors.
Switched it off and created nothing The commonest case of all. The tasks simply stop running, with no error line anywhere.
A multisite network Every site in the network has its own scheduler. One call is not enough: the curl form has to be repeated for each site’s address.

The server time may not be yours, and that changes everything you schedule: what time the server is on.

Got the command written and the task never runs? Send us the line and the domain.

Open a request

SEE ALSO

WordPress hosting

Hosting plans

Support Policy

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $10.00/mo

See plans
  • 0 Users Found This Useful
Was this answer helpful?