admin管理员组

文章数量:1122846

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

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
Share Improve this question edited Apr 12, 2024 at 5:13 Jesse Nickles 7357 silver badges19 bronze badges asked Jul 19, 2017 at 13:45 user124167user124167 111 silver badge2 bronze badges 7
  • 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 Commented Jul 19, 2017 at 14:19
  • Yes. Is the path parameter you mention path of WPCLI? – user87795 Commented 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 Commented Jul 19, 2017 at 14:56
  • 1 Please merge your accounts. – Dave Romsey Commented 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. – Daniel Bachhuber Commented Jul 24, 2017 at 13:25
 |  Show 2 more comments

1 Answer 1

Reset to default 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).

本文标签: wp cliWPCLI command to update plugins and core fails in crontab