Italian (Italy)
Knowledge Base  >  Domande Generali  >  WordPress WP-Cron Scheduled Tasks  >  How WP-Cron executes your Cron Jobs

Every WordPress website supports WP-Cron. There is nothing that should be installed to support this feature, because WP-Cron is the native WordPress framework for executing scheduled tasks.

What is exactly a "scheduled task" or a "cron job"?

First off, a "cron job" is actually a "scheduled task". It's a specific action that is registered to be executed at a certain interval of time, like "once every day". Cron Jobs are extremely useful to configure tasks/actions that should run automatically every day, or even with a higher frequency. For example, sending an automatic email reminder to your customers is a classic example of when Cron Jobs come handy. All our Vik plugins support Cron Jobs, and they allow you to schedule pre-installed or even custom actions to be executed automatically by your WordPress website or by your own server.

WordPress plugins can schedule tasks within the WP-Cron framework by registering the action to be performed and the execution interval (also called "recurrence interval"). WordPress itself registers by default tens of events (tasks) that should run automatically once hourly, once daily, once weekly etc... to perform useful actions such as installing automatic plugin updates, checking for new core updates, deleting expired article drafts or transients, keeping the recovery mode keys active, and much more. These default actions/events ensure the correct functioning and health of your WordPress website.

Just like how WordPress schedules some default tasks to run automatically within WP-Cron, plugins can do the same thing by scheduling their own custom tasks/events. We suggest installing a popular and free WordPress plugin called WP Crontrol that allows you to check and manage all the events scheduled with WP-Cron.

How does WP-Cron execute the scheduled tasks?

By default, your WordPress installation runs whenever a user or a bot visits your website address (URL). In case your website has got no visitors, then your web-server will NOT be able to execute the WordPress PHP files that would trigger the execution of WP-Cron. Technically speaking, WP-Cron runs by default at "application level", which means whenever someone visits a page of your website and starts the WordPress application. This is because WordPress is made of PHP files that are executed by your web-server (Apache, Nginx etc..) on your hosting.
As a result of this technical requirement of having a visitor (a browser) or a bot visiting your website URLs and triggering WordPress to start, the WP-Cron scheduled tasks (cron jobs) may not be executed every day at the same time, or the desired execution interval may not be reflected. If you don't have anyone visiting your website and starting the WordPress application, then WP-Cron will not run, hence your scheduled tasks will not be executed on time or at all.

However, this is not the only way for executing or running WP-Cron. As we said, WP-Cron is a PHP application of WordPress, and so as such, it requires a process that makes WP-Cron run. If you don't want to rely on website visitors (bots or real users), then you should configure a real Cron Job on your own server that will make WP-Cron run at "server level". Most hosting companies offer a Control Panel to access several tools and settings, among which you should be able to find an apposite tool to manage your "Cron Jobs" (also called "Scheduled Tasks"). You should ask your hosting company if and how custom Cron Jobs can be configured on your hosting plan/server.

A "Cron Job" configured on your own server is actually a real task scheduled for automatic execution, whose execution is guaranteed by your own server, unlike the scheduled tasks configured in WP-Cron that cannot be guaranteed to be executed at the desired intervals for the above mentioned characteristics of the "application level" execution.

Most servers running Unix-like operating systems (i.e. Linux) come with the cron command-line utility for scheduling the regular execution of "jobs" (commands) to run periodically at fixed times, dates, or intervals. This is done through a configuration file called "crontab" (cron table), where most hosting companies allow you to manage such configuration through a dedicated page/tool on your server Control Panel. Therefore, rather than having to write cron commands on the crontab file, you will probably be able to use the User Interface tool provided by your hosting company in the Control Panel to schedule the desired jobs.

How to let your server run WP-Cron?

Scheduling the execution of WP-Cron at "server level" is the best way to ensure its execution at an exact time, date or interval. Your hosting company may provide some help articles showing how to properly configure a server-side Cron Job for your WordPress website to run WP-Cron, and so we strongly suggest checking what are the available options with your hosting company. In case they do not provide any specific documentation or instruction, this is an operation that can always be performed by knowing how WordPress runs WP-Cron, which is basically a PHP file available on the root directory of your WordPress installation called wp-cron.php. The official WP-Cron description and documentation is available at https://developer.wordpress.org/plugins/cron/.

In order to have your own sever run WP-Cron automatically, you need to let the WP-Cron PHP file be executed. This is the command you want to schedule for automatic execution on your server control panel through the crontab configuration.

The WP-Cron PHP file called wp-cron.php can be executed in many different ways by your own server, but the most common methods are just two:

  1. By making a ping to the full file URL of your website, such as https://yourwebsite.com/wp-cron.php. This is technically called a GET request to a URL.
  2. By letting the PHP interpreter run the file wp-cron.php.

You should choose the best action/command to schedule for automatic execution on your own server as a Cron Job. A valid example for the first method (GET request) is the one below (make sure to replace the URL with your WordPress website address):

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

This command requires the CLI executable file wget to be available on your server, but this is a common package among Linux hostings. The above command makes a GET request to the exact URL where WP-Cron will be executed in the proper way, and it will turn off email notifications for its execution.

Instead, a valid server-side command for crontab to execute the file through the PHP interpreter is the one below (make sure to adjust the path to the PHP interpreter as well as the server path to the .php file):

/usr/bin/php /path/to/website/root-directory/wp-cron.php

Please notice that the above example of commands will only work if your hosting company provides a tool on their server control panel to schedule custom Cron Jobs. You should then be asked to choose the execution interval, like "every hour", "once daily", "once every week" etc.. As long as your server control panel provides an interface/tool to schedule custom Cron Jobs, you will be asked to choose the execution interval as well as to enter the command to be executed.
Also, you don't need to schedule one command for each custom scheduled task on your WordPress website, because letting WP-Cron run will automatically execute all the scheduled tasks and cron jobs. Therefore, one command at server level is sufficient to ensure the regular execution of WP-Cron.

Instead, if your hosting company does NOT provide any tools to manage your custom Cron Jobs, then you will have to edit the cron table configuration file directly on your server, usually by connecting to your own server via SSH through a shell (terminal) interface, and by launching the command crontab -l or crontab -e. In this case, the commands alone to be executed would NOT be sufficient, as crontab requires the signature to define the execution interval of such commands, for example:

// every day at 9:45
45 9 * * * wget -q -O - https://yourwebsite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

// every hour at the minute 30
30 * * * * wget -q -O - https://yourwebsite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

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

We strongly recommend to contact your hosting company in order to properly configure crontab or how to properly use their control panel interface for scheduling one or more custom Cron Jobs. You can find more details about "cron" and "crontab" at https://en.wikipedia.org/wiki/Cron. For sure, if your hosting company provides a tool on their Control Panel to manage the Cron Jobs, then scheduling the proper commands would be easier, and you will only need to provide the command and to choose the execution interval through their interface, rather than having to write the crontab-compliant tasks in the server's configuration file.

How to disable WP-Cron from running at "application level"?

Although this is not recommended, because multiple executions of WP-Cron would not harm your WordPress website, and because WP-Cron is enabled at application level by default, it is possible to actually disable the regular application from running whenever someone visits your website.

This can be done by adding a specific PHP constant to the WordPress wp-config.php configuration file, and this should be done ONLY if you have previously set up a server-side cron job that will make WP-Cron run. This is the constant that WordPress requires in order to not execute WP-Cron when someone visits your website, and you should add the following line of code to your wp-config.php file on the root directory:

define('DISABLE_WP_CRON', true);

Conclusion

Setting up the execution of WP-Cron at server-level is strongly recommended, because this is how your server will be instructed to execute a real Cron Job that will make the WP-Cron framework run and process all of your tasks that were scheduled by your plugins. Instead, disabling WP-Cron at application level is NOT needed, and this is because running multiple times the same Cron Jobs scheduled in WP-Cron will not produce duplicate results as long as the scheduled tasks perform the operations correctly. If you are using our Vik plugins, then it is safe to keep WP-Cron enabled at application level and still schedule its regular execution at server level.

Ultimo aggiornamento: 2024-05-29
Utile?