The Problem

WP-CLI (wp command) works perfectly in your terminal but fails with "command not found" when run from cron jobs, systemd timers, or deployment scripts. This is because cron runs with a minimal PATH that does not include the WP-CLI installation directory.

Symptoms

  • wp command works in SSH terminal
  • Same command in crontab fails with "wp: command not found"
  • Deployment scripts fail at WP-CLI steps
  • Cron emails contain "wp: not found" errors
  • Systemd service fails to start WP-CLI commands

Real Error from Cron

``` /bin/sh: 1: wp: not found

Or: /opt/wp-cli/wp: /usr/bin/env: bad interpreter: No such file or directory ```

How to Fix It

Fix 1: Use Absolute Path to WP-CLI

```bash # Find the full path which wp # Output: /usr/local/bin/wp

# Use absolute path in crontab */5 * * * * cd /var/www/html && /usr/local/bin/wp cron event run --due-now ```

Fix 2: Set PATH in Crontab

```bash # At the top of crontab (crontab -e) PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

*/5 * * * * cd /var/www/html && wp cron event run --due-now ```

Fix 3: Source Profile in Cron Script

```bash #!/bin/bash # /usr/local/bin/wp-backup.sh

# Load environment source /root/.bashrc # OR source /etc/profile

# Now wp command should be available cd /var/www/html wp db export /backups/db-$(date +\%Y\%m\%d).sql ```

Fix 4: Install WP-CLI in Standard Location

```bash # Download and install WP-CLI to /usr/local/bin (in PATH) curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar chmod +x wp-cli.phar sudo mv wp-cli.phar /usr/local/bin/wp

# Verify it's in PATH which wp # Should return: /usr/local/bin/wp ```

Fix 5: Use WP-CLI Phars Direct Path

```bash # If WP-CLI is installed as a phar file WP_CLI=/opt/wp-cli/wp-cli.phar

*/5 * * * * cd /var/www/html && /usr/bin/php $WP_CLI cron event run --due-now ```

Fix 6: Verify PHP Path for Cron

```bash # Find PHP path which php # Output: /usr/bin/php

# Use in cron */5 * * * * cd /var/www/html && /usr/bin/php /usr/local/bin/wp cron event run --due-now ```

Fix 7: Complete Cron Example

```bash # /etc/cron.d/wordpress SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin MAILTO=admin@example.com

# Run WP-Cron every 5 minutes */5 * * * * www-data cd /var/www/html && /usr/local/bin/wp cron event run --due-now --quiet

# Daily database backup at 2 AM 0 2 * * * www-data cd /var/www/html && /usr/local/bin/wp db export /backups/db-$(date +\%Y\%m\%d).sql --quiet

# Weekly plugin updates check 0 3 * * 0 www-data cd /var/www/html && /usr/local/bin/wp plugin list --update=available --format=csv > /tmp/plugin-updates.csv ```