如何让 wp-cli 命令在 cron 的 bash 脚本中工作

How to have wp-cli commands work in a bash script from cron

我有一个 bash 脚本,其中包含各种 WP-CLI 命令,用于 运行 备份、检查和优化数据库表、运行 安全漏洞检查、检查可用核心、插件和主题更新,然后将报告通过电子邮件发送给我。

如果我从命令行 运行 这个脚本,它会完美运行。但是,如果我安排一个 cronjob,脚本 运行s,但是所有 WP 命令的输出都是这样的:

/usr/local/bin/dev-maintenance-check.bash: line 70: wp: command not found

我尝试了各种不同的 cron 作业格式,但都无济于事。这是当前的 cronjob:

0 1 * * 0,3 bash /usr/local/bin/dev-maintenance-check.bash

我的 WP-cli 安装在 usr/local/bin/wp。同样,如果我在命令行中 运行 "bash /usr/local/bin/dev-maintenance-check.bash",那是有效的。

对我来说,似乎需要在脚本中调用 wp-cli。我确信我已经用尽了对 wp-cli 文档的 Google 的搜索。任何帮助将不胜感激。

As your script is a shell script (/bin/sh), then your PATH entries in .bashrc will not be read as that is for the bash (/bin/bash) interactive shell.

To make your PATH entries available to /bin/sh scripts run by a specific user, add the PATH entry to the .profile file in that users home directory.

https://unix.stackexchange.com/a/163126/72176


所以,有趣的部分不是你的 crontab,而是你的脚本。只需调用 wp 的绝对或相对位置或按照上面的建议修复它,它应该可以工作。

绝对值:

#!/usr/bin/env bash
cd /var/www/wordpress
/usr/local/bin/wp cache flush

相对(WP-CLI 作为本地 Composer 依赖安装):

#!/usr/bin/env bash
cd /var/www/wordpress
vendor/bin/wp cache flush