If you want to disable the “fake” WordPress cron and trigger your scheduled tasks, then you will need to use a real cron job to ping WordPress.
How to disable WP Cron
Just add a single line to your instances wp-config.php
file:
define( 'DISABLE_WP_CRON', true );
How to set up a real cron job
The easiest might be to just open you hostings configuration user interface and click it together. In case you are like me not really satisfied by a manual click-job, you can use the crontab
command on your server. You can search it up on your own, here are the basics:
- SSH into your server
- Figure out the user who owns the PHP process.
ps -efj
will tell you the process and the user who owns it - Enter
crontab -u $PHP_USER_NAME -l
to see the existing jobs (backups for e.g.) for this user - Add a new cronjob to ping your server – some examples
How to run WP Cron
Now you have to decide if you want to ping WordPress via HTTP or via the filesystem. Your options are: php
, curl
/wget
.
In case you go with PHP, check whereis php
to get the full path to the PHP executable. This way you do not have to rely on the global and the PATH environment variable:
/usr/local/bin/php /var/www/wp/wp-cron.php
In case you use wget
:
wget -qO - https://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
That’s pretty much it. Keep in mind that the ?doing_wp_cron
query arg is important as cron will else not run.
Additional info
The crontab has a built in editor: crontab -e
so you can type things fullscreen.
In case your cronjob throws an error (because there’s an echo
or other things that should not be there) and you just want to test things, you can route it to >/dev/null 2>&1
, therefore hiding its output and errors. You do not have to do that.
Alternatives
When you have a very long running task or a task with an unpredictable running time – like importing things, reworking images, converting mp3 or mp4 files – then you might have a still running cronjob while there is already another one started, therefore failing. You can prevent that by using a crontab alternative like runwhen that has a job queue or, if you like the Go Language, use Jobber.
Another alternative is to use a uptime monitoring service. Those are available for free and you can directly ping your server (see above wget
example) without doing any setup.