Introduction
WP-CLI works perfectly in interactive shell sessions but fails when executed from cron jobs with "command not found" or "Could not find wp-config.php" errors. This is because cron runs with a minimal environment that lacks the PATH entries and working directory context that WP-CLI requires:
```bash # Works in interactive shell wp plugin list
# Fails in cron: /bin/sh: wp: command not found # Or: Error: This does not seem to be a WordPress installation. ```
Symptoms
- Cron job email output shows "wp: command not found"
- Scheduled WP-CLI tasks silently fail (no output, no execution)
- WP-CLI works when run manually but not from crontab
- Error: "Could not find wp-config.php" even with correct
--pathargument - PHP binary path differs between interactive shell and cron environment
Common Causes
- Cron's PATH does not include the directory where
wpbinary is installed - WP-CLI installed via Composer in
vendor/bin/wpwhich is not in cron PATH - Cron runs with a different user whose home directory differs
- Working directory not set, so WP-CLI cannot find wp-config.php
- PHP binary in cron environment differs from the interactive shell PHP
Step-by-Step Fix
- 1.Find the absolute path to the wp binary:
- 2.```bash
- 3.which wp
- 4.# Output: /usr/local/bin/wp
- 5.
` - 6.Use absolute paths in your crontab:
- 7.```bash
- 8.# WRONG
- 9.*/15 * * * * wp cron event run --due-now
# CORRECT */15 * * * * /usr/local/bin/wp cron event run --due-now --path=/var/www/html
# Even better, set variables at the top of crontab WP=/usr/local/bin/wp WP_PATH=/var/www/html */15 * * * * $WP cron event run --due-now --path=$WP_PATH > /dev/null 2>&1 ```
- 1.Set PATH in the crontab:
- 2.```bash
- 3.PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- 4.*/15 * * * * cd /var/www/html && wp cron event run --due-now > /dev/null 2>&1
- 5.
` - 6.Create a wrapper script for complex WP-CLI cron jobs:
- 7.```bash
- 8.#!/bin/bash
- 9.# /usr/local/bin/wp-cron-runner.sh
- 10.export PATH="/usr/local/bin:/usr/bin:/bin:$PATH"
- 11.WP="/usr/local/bin/wp"
- 12.WP_PATH="/var/www/html"
cd "$WP_PATH" || exit 1
"$WP" cron event run --due-now --allow-root >> /var/log/wp-cron.log 2>&1
``
Then in crontab:
bash
*/15 * * * * /usr/local/bin/wp-cron-runner.sh
- 1.Specify the correct PHP binary if your system has multiple PHP versions:
- 2.```bash
- 3./usr/bin/php8.2 /usr/local/bin/wp cron event run --due-now --path=/var/www/html
- 4.
` - 5.Or set the
WP_CLI_PHPenvironment variable: - 6.```bash
- 7.export WP_CLI_PHP=/usr/bin/php8.2
- 8.
`
Prevention
- Always use absolute paths in cron jobs, never rely on PATH resolution
- Document all WP-CLI cron jobs with their full commands in your server runbook
- Use wrapper scripts for any cron job that requires multiple commands or environment setup
- Test cron jobs by running them as the cron user:
sudo -u www-data crontab -l - Log WP-CLI cron output to a file for debugging:
>> /var/log/wp-cron.log 2>&1 - Monitor cron execution by checking the exit code and log file after each run
- Add a health check that verifies the last cron run time:
wp cron test