You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在文档根目录下自动升级所有WordPress实例(>=4.7.x)的最简方法

针对你在Ubuntu 16.04 + Nginx环境下管理多个轻量WordPress站点的场景,想要简化现有自动升级流程的话,其实可以利用WP-CLI的原生能力和系统工具,把原来的三个循环简化成更简洁的批量处理逻辑,同时精准过滤出版本≥4.7.x的站点。

最简实现思路

核心是用find命令自动定位所有WordPress实例(通过wp-config.php文件),结合WP-CLI的批量升级命令,再用系统自带的版本比较工具过滤符合要求的站点,完全不需要手动维护站点列表。

简化后的cron_daily.sh脚本

替换你现有的脚本内容,下面是完整实现:

#!/bin/bash
# 加载外部声明的文档根目录变量${drt}
source /path/to/your/env-vars.sh # 替换成实际加载${drt}的文件路径

# 遍历所有包含wp-config.php的目录(即WordPress根目录)
find "${drt}" -name wp-config.php -type f | while read -r config_file; do
    wp_root=$(dirname "${config_file}")
    
    # 获取当前WordPress版本并检查是否≥4.7.x
    current_version=$(wp core version --path="${wp_root}" --quiet)
    if dpkg --compare-versions "${current_version}" "ge" "4.7"; then
        echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting update for ${wp_root} (v${current_version})"
        
        # 升级WordPress核心
        wp core update --path="${wp_root}" --quiet
        
        # 升级所有激活/已安装的插件
        wp plugin update --all --path="${wp_root}" --quiet
        
        # 升级所有主题(不需要的话可以注释掉这行)
        wp theme update --all --path="${wp_root}" --quiet
        
        # 可选:清理旧的核心安装包,释放空间
        wp core cleanup --path="${wp_root}" --quiet
        
        echo "[$(date '+%Y-%m-%d %H:%M:%S')] Update completed for ${wp_root}"
    else
        echo "[$(date '+%Y-%m-%d %H:%M:%S')] Skipping ${wp_root}: v${current_version} is older than 4.7.x"
    fi
done

关键优化点说明

  • 自动定位站点:用find "${drt}" -name wp-config.php自动扫描所有WordPress实例,不用手动添加站点路径到循环里,新增站点会自动纳入升级范围。
  • 精准版本过滤:用Ubuntu自带的dpkg --compare-versions做版本比较,比自己写字符串匹配逻辑更可靠,支持标准的版本号格式。
  • 简化升级流程:WP-CLI的wp core update、wp plugin update --all、wp theme update --all已经是批量处理命令,省去原来的多个循环逻辑。
  • 日志友好:加上时间戳输出,方便后续排查升级情况。

Crontab配置(保持原有定时逻辑)

确保脚本有可执行权限:

chmod +x /path/to/cron_daily.sh

然后保持每天执行的定时任务(比如凌晨2点,避免高峰),并输出日志到文件:

0 2 * * * /path/to/cron_daily.sh >> /var/log/wp-auto-updates.log 2>&1

可选的额外建议

  • 备份前置:如果担心升级出问题,可以在升级前添加数据库备份步骤,比如在版本检查通过后加:
    wp db export --path="${wp_root}" --quiet "${wp_root}/wp-backup-$(date '+%Y%m%d').sql"
    
  • 权限检查:确保执行脚本的用户(比如www-data或你当前的用户)对所有站点目录有读写权限,避免升级失败。
  • 排除特定站点:如果有个别站点不需要自动升级,可以在find命令里排除,比如:
    find "${drt}" -name wp-config.php -type f ! -path "${drt}/special-site/*" | while read -r config_file; do
    

内容的提问来源于stack exchange,提问作者Arcticooling

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:28:47