1

I have the following WP-CLI command to update all plugins and core. It seems to fail when being executed from cron (other cron commands work):

0 0 * * 0 for dir in /var/www/html/*/; do cd "$dir" && wp plugin update --all --allow-root; wp core update --allow-root; done

I tried to run it manually (did so after a manual update from GUI after a period without updates), I got the following for all sites:

Success: Plugin already updated.
Success: WordPress is up to date.

Why would the command fail on cron?

I tried to debug this by letting the command run each minute (* * * * *) and checking the output (cron output usually goes to /var/mail/root).

/var/mail/root output:

Subject: Cron <root@machine_name> for dir in /var/www/html/*/; do cd "$dir" && wp plugin update --all --allow-root; wp core update --allow-root; done
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
Message-Id: <20170723050601.E8FBF3EF4F@machine_name>
Date: Sun, 23 Jul 2017 05:06:01 +0000 (UTC)

/bin/sh: 1: wp: not found
/bin/sh: 1: wp: not found
/bin/sh: 1: wp: not found
/bin/sh: 1: wp: not found
/bin/sh: 1: wp: not found
/bin/sh: 1: wp: not found
/bin/sh: 1: wp: not found
/bin/sh: 1: wp: not found
/bin/sh: 1: wp: not found
/bin/sh: 1: wp: not found
6
  • 1
    Are you sure it runs on every folder? Also instead of changing the working directory via cd, consider using the --path="" parameter
    – Tom J Nowell
    Jul 19, 2017 at 14:19
  • Yes. Is the path parameter you mention path of WPCLI?
    – user87795
    Jul 19, 2017 at 14:38
  • 2
    Yes., path is part of standard WP-CLI. I'm not a fan of such one-liners, I placed basically the same things in a simple bash script, which will be executed by cron - much easier to debug
    – kero
    Jul 19, 2017 at 14:56
  • 1
    Please merge your accounts. Jul 23, 2017 at 8:07
  • Can you share how you've installed WP-CLI? The /bin/sh: 1: wp: not found error implies the wp executable isn't found when cron is running. If you're using a bash alias to reference WP-CLI, or your bash profile to modify $PATH, then neither will be loaded automatically in the cron context. Jul 24, 2017 at 13:25

1 Answer 1

1

Understanding this answer requires preliminary knowledge in a system administration and Linux issue named "Environment variables". Acquiring this knowledge could be done with a didactic Linux book, course, or tutor. If one explanation was bad, seek another.


The problem and the solution:

It seems to happen due to a partial utilization of the PATH environment variable, by cron.

When cron runs it has only the /usr/bin value of this variable, instead the whole set, common in Ubuntu 16.04:

/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

What I did was to add the full location of WP-CLI to the commands (I also splitted the long one-liner command to two short commands):

From:

0 0 * * * for dir in /var/www/html/*/; do cd "$dir" && wp plugin update --all --allow-root; done
0 0 * * * for dir in /var/www/html/*/; do cd "$dir" && wp core update --allow-root; done

To:

0 0 * * * for dir in /var/www/html/*/; do cd "$dir" && /usr/local/bin/wp plugin update --all --allow-root; done
0 0 * * * for dir in /var/www/html/*/; do cd "$dir" && /usr/local/bin/wp core update --allow-root; done

Note the /usr/local/bin/ right before wp.

To test this works without waiting a whole day I changed the cron schedule from 0 0 * * * (in the first minute, of the first hour, in each day of month, in each month, in each day of week), to this:

* * * * *

(in each minute, in each hour, in each day of month, in each month, in each day of week).

After about 2 minutes I checked one of my websites and saw everything was updated (besides themes, which updating them isn't included in the two commands I used).

3
  • 1
    upvoted, but why not to just write a shell script like kero sugested in the comments? Jul 30, 2017 at 13:37
  • 1
    Running wp-cli as root introduces a nightmare's worth of security holes as essentially you are executing code from the user websites as root. This is horribly dangerous and would result in your box being owned. You must run wp-cli as the end user. If you leave it as it is, it's only a matter of time before it's exploited.
    – Brian C
    Feb 26, 2021 at 23:50
  • 1
    Also worth noting that many versions of cron/crontab will allow you to change the PATH with a PATH line, thus removing the need for full paths. With this length of script, I'd usually embed it in a shell script, if only to make error checking simpler.
    – Brian C
    Feb 26, 2021 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.