Quick trick – running wp-cli command via Cron Job

Today i faced strange issue with WP-Cli command.

I have simple php script (let’s call it task.php) which runs through Crontab. All blocks of main procedure were running OK except one. That one with

shell_exec("cd /var/www/appname;wp COMMAND_HERE");

Nothing special, just one line of simple shell command.

I run the script in SSH Console

cd /var/www/appname; php task.php

and everything works OK, included shell_exec line.

But same script behaves differently when it is called from cronjob.

After quick investigation i noticed that it was because of WP command.

In cronjob the system doesn’t recognize this command for some already known reasons for me.

That’s why i had to set full path of WP command. And it started working.

Thus, if you face such problem, just replate this

shell_exec("cd /var/www/appname;wp COMMAND_HERE");

with this

shell_exec("cd /var/www/appname; /usr/local/bin/wp COMMAND_HERE");

And it will start working OK.

Get more useful WP tricks and snippets by subscribing to my mailclub.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.