If you’ve tried running WP-CLI commands through cPanel’s cron jobs and encountered errors like:
WP-CLI only works correctly from the command line, using the 'cli' PHP SAPI.
You're currently executing the WP-CLI binary via the 'cgi-fcgi' PHP SAPI.You’re not alone. This issue stems from how cPanel handles PHP execution — often defaulting to FastCGI (cgi-fcgi) instead of the required CLI SAPI. Here’s how to fix it using the wp-cli.phar file and PHP CLI directly.
Step 1: Download WP-CLI Phar
If you haven’t already, download the WP-CLI Phar file to your home directory:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar ~/wp-cli.pharStep 2: Create a Shell Script
Create a script that runs your WP-CLI command using the correct PHP binary. For example:
#!/bin/bash
# Log the cron execution time
echo "Cron ran at $(date)" >> ~/logs/cron-test.log
# Run WP-CLI using PHP CLI
/usr/local/bin/php ~/wp-cli.phar test-script --path=~/public_html | while IFS= read -r line; do
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $line"
done >> ~/logs/test.log 2>&1Save this as test-script.sh in ~/scripts/ and make it executable:
chmod +x ~/scripts/test-script.shStep 3: Set Up the Cron Job in cPanel
In your cPanel dashboard:
- Go to Cron Jobs
- Set the desired schedule (e.g., every 2 days at 6 AM):
0 6 */2 * * /bin/bash /home/yourusername/scripts/test-script.shAnd save the cron job
Step 4: Verify Execution
Check your logs after the scheduled time:
cat ~/logs/cron-test.log
cat ~/logs/test-script.logTroubleshooting Tips
- Make sure you’re using the correct PHP CLI binary. Run
php -vand confirm it says(cli)in the output. - Avoid using the
wpbinary directly in cron — it may default to the wrong PHP SAPI. - Always test your script manually before relying on cron:
/bin/bash ~/scripts/test-script.sh