☰
WORDPRESS / PRACTICE

How to Reliably Run WP-Cron Through System Cron

How to Reliably Run WP-Cron Through System Cron

WP-Cron in WordPress is not a separate system daemon that runs independently of the site on a fixed schedule. With the default configuration, WordPress checks for scheduled events when pages are visited and attempts to start the cron handler through an HTTP request.

This is often sufficient for small websites, but projects with uneven traffic can experience delays, repeated attempts, and overlapping runs. A more reliable production approach is to disable automatic WP-Cron execution during site visits and trigger the queue through system cron, preferably with a lock to prevent simultaneous execution.

Why Standard WP-Cron Can Be Unreliable

WordPress stores scheduled events in its queue, but it does not run them exactly at the time specified in the schedule. An event is processed when the site receives a suitable request and WordPress performs a cron check.

On a low-traffic site, this causes delays: an email campaign, import, or cache cleanup may start dozens of minutes after its scheduled time. During a sudden traffic increase, the problem is different: several HTTP requests may attempt to start the same background tasks almost simultaneously.

Additional risks occur when a cron task runs for a long time, depends on an external API, or processes a large number of records. In this situation, a new run may begin before the previous one has finished if the plugin or its locking mechanism does not protect against duplication.

  • Scheduled events run late when the site receives no visits.
  • Parallel HTTP requests increase the load on PHP and the database.
  • Long imports, email queues, and synchronizations may run more than once.
  • Troubleshooting becomes more difficult because cron depends on random user activity.

How to Disable WP-Cron During Site Visits

First, open the wp-config.php file in the root directory of the WordPress installation. Add the constant before the line where WordPress loads wp-settings.php. It must be declared before that include so WordPress reads it during startup.

Create a backup of the configuration file and check the syntax after editing it. An incorrect change to wp-config.php can make the site inaccessible, especially if an extra quotation mark or a missing semicolon is introduced.

  • Do not add the constant multiple times in different places in the file.
  • Make sure system cron is configured before disabling automatic execution.
  • Do not use the value false: disabling WP-Cron requires true.
define('DISABLE_WP_CRON', true);

Configuring System Cron with WP-CLI

Use WP-CLI, the WordPress command-line interface, for execution. It is important to run the command as a user who has access to the site files and the required PHP environment. On many servers, this is the web server user, such as www-data, but the exact name depends on the operating system and hosting configuration.

Using the web server user's crontab is preferable to running the command as root: files, caches, and logs will not receive the wrong owner. If WP-CLI or WordPress relies on environment variables, PHP paths, the home directory, or another profile, account for them explicitly in cron because the cron environment is usually minimal.

  • Check the WP-CLI path with which wp as the required user.
  • Confirm that the directory specified by --path contains the intended WordPress installation.
  • Use absolute paths if the command cannot find wp or PHP.
  • Make sure the user can read wp-config.php and write files required by plugins.
*/5 * * * * flock -n /tmp/wp-cron.lock wp --path=/var/www/site cron event run --due-now --quiet

Why flock Is Needed

flock creates a lock for the duration of the command. The -n option means that a new process will not wait for the previous one to finish: if the lock is already held, the command exits immediately. This prevents handlers from overlapping when a previous run is still performing a long import or sending a message queue.

The lock file is not an execution log and normally does not require manual cleanup. The lock is released when the process ends. However, check the permissions on the directory where the lock file is created. On systems where /tmp has non-standard restrictions, you can use a separate directory with permissions for the cron user.

  • A five-minute interval is an example, not a universal requirement.
  • Running the task more frequently will not speed up a task that already takes longer than the interval.
  • flock protects against parallel runs of this cron process, but it does not fix duplication built into a specific plugin's logic.

How to Add the Task as the Web User

Depending on the server, system cron is configured through the required user's crontab or through the operating system's scheduler configuration. Before adding the task, make sure the command runs manually as the same user. This helps identify permission, PATH, and PHP version problems before switching to automatic execution.

  • Do not run WordPress commands as root unless necessary.
  • If WP-CLI is not installed in the standard PATH, specify the full path to the executable.
  • For troubleshooting, temporarily remove --quiet or redirect stdout and stderr to a log.
sudo -u www-data wp --path=/var/www/site cron event run --due-now

Checking the Queue and WP-Cron Functionality

After configuration, do not limit verification to checking the crontab entry. Confirm that WordPress can see the events, WP-CLI can connect to the intended site, and the commands actually execute overdue tasks.

The event list shows hook names, the next scheduled run time, and other available information. This helps identify events that remain in the queue, run too frequently, or belong to a plugin that has already been removed.

The wp cron test command checks whether WordPress can perform the basic cron mechanism. Read its result together with web server and PHP logs: an error may be related to loopback requests, DNS, SSL, authentication, or network restrictions.

  • View the queue: wp cron event list.
  • Test the cron mechanism: wp cron test.
  • Run overdue events manually: wp cron event run --due-now.
  • Run the check again afterward to see whether the queue is decreasing.
wp --path=/var/www/site cron event list
wp --path=/var/www/site cron test
wp --path=/var/www/site cron event run --due-now

Logs, Errors, and Load Control

The --quiet flag removes normal command output, which is convenient in production but can hide useful messages during troubleshooting. During diagnostics, run the command manually without this flag and check the exit code.

A system cron setup should include logging. Logs may reveal database access errors, missing PHP extensions, external service timeouts, or file permission problems. Do not leave verbose logging enabled indefinitely without controlling its size, or the log may consume available disk space.

If cron regularly fails to finish before the next interval, do not automatically increase the execution frequency. First identify the event that takes the longest, review its logic, and, where possible, split the large operation into smaller batches.

  • Check system cron and PHP logs.
  • Monitor database and external API errors.
  • Track the execution time of imports, email campaigns, and synchronizations.
  • Do not create multiple independent cron rules for the same site unless necessary.
*/5 * * * * flock -n /tmp/wp-cron.lock wp --path=/var/www/site cron event run --due-now >> /var/log/wp-cron.log 2>&1

Common Mistakes When Switching to System Cron

The most common mistake is disabling WP-Cron in wp-config.php without adding a working system cron job. As a result, events remain in the queue and plugins stop performing background actions. Test WP-CLI manually first, then add cron, and only after that leave DISABLE_WP_CRON enabled.

Another issue is using the wrong site path. A server may contain several WordPress installations, and a command without --path may target the wrong directory or fail.

Do not keep a separate wp-cron.php call through the web server, a system cron job using WP-CLI, and a third-party scheduler running at the same time. This creates multiple execution sources and makes load analysis more difficult. Choose one controlled mechanism.

Also check the time zone. System cron and WordPress may use different time settings, so the time shown in the event list should not always be interpreted as the server's local time.

  • Do not confuse system cron with WP-Cron.
  • Do not run the same site under different system users without a clear reason.
  • Do not ignore permissions for the lock file and WordPress files.
  • Do not delete an event from the queue before determining which plugin created it.

When System Cron Is Not a Complete Solution

Moving execution to system cron solves the start-time problem and helps prevent overlapping handlers, but it does not guarantee that third-party plugins will work correctly. If a plugin creates unlimited events, fails to handle errors, or repeats unsuccessful requests, that issue must be fixed separately.

Very large queues may require specialized workers, message queues, or configuration changes within the plugin itself. WP-CLI also cannot bypass memory limits, database timeouts, or external API limits.

After switching to system cron, regularly check the event list and logs. Stable operation does not mean that the logs contain no entries at all; it means tasks run predictably without a continuously growing backlog of overdue events.

  • Long-running tasks should be optimized or split into batches.
  • External APIs may have their own limits and temporary errors.
  • Critical tasks require separate alerts for failed execution.

Frequently asked questions

Should DISABLE_WP_CRON remain enabled after configuring system cron?

Yes, if you want system cron to be the only execution method. The constant disables automatic attempts during site visits, while WP-CLI continues to run scheduled events.

Why is system cron run as the web server user?

This user typically has the required access to the files and WordPress environment. Running commands as root can create files with the wrong owner and cause problems for the site.

What should I do if wp cron event list shows many overdue events?

First, run the overdue events manually and review the errors. If the queue quickly builds up again, identify the long-running or faulty task, check the plugin, and measure the execution time of individual hooks.

Is using flock mandatory?

It is not always critical for simple, short tasks, but for production sites with long imports, email campaigns, or synchronizations, a lock is useful protection against overlapping runs.

Conclusion

System cron makes WordPress task execution predictable: it does not depend on site visits, and the interval can be controlled at the server level. The combination of DISABLE_WP_CRON, WP-CLI, and flock is particularly suitable for production sites with background imports, email campaigns, and other long-running operations.

After configuration, check user permissions, the WordPress path, the event list, logs, and the absence of parallel execution mechanisms. This gives you more than a cron job that runs: it provides a controlled task queue with clear diagnostics.

← All WordPress materials