Autoupdate WP-CLI and packages with Linux Cronjob

WP-CLI is adding more useful functionality every day to help sysadmins and hosts make WordPress management a breeze. I have found myself having to update WP-CLI manually over SSH for all of my sites which is pretty monotonous. This tutorial will show you how to autoupdate WP-CLI and its packages with a Linux cronjob on Debian and Ubuntu. The instructions should be fairly easy to adapt for CentOS and other Linux distros.

If you want a WordPress host with WP-CLI check out Kinsta and Cloudways.

Autoupdate WP-CLI and packages with Linux Cronjob

Updating WP-CLI uses the update command

wp cli update --allow-root

You will get a confirmation prompt to update that you have to enter y to proceed

You have version 1.3.0. Would you like to update to 1.5.0? [y/n]

We can automatically pass the y confirmation by using echo.

echo "y" | wp cli update --allow-root

The WP-CLI update command also has a --yes flag for autoconfirming the update (thanks for the reminder Pothi 🙂 )

wp cli update --yes --allow-root

Updating WP-CLI packages uses the package update command

wp package update --allow-root

You will see output similar to this showing the checks and relevant updates running

Installing package wp-cli/doctor-command (dev-master)
Updating /root/.wp-cli/packages/composer.json to require the package...
Using Composer to install the package...
---
Loading composer repositories with package information
Updating dependencies
Resolving dependencies through SAT
Dependency resolution completed in 0.143 seconds
Analyzed 5463 packages to resolve dependencies
Analyzed 333650 rules to resolve dependencies
Package operations: 1 install, 0 updates, 0 removals
Installs: wp-cli/doctor-command:dev-master 9656de3
 - Installing wp-cli/doctor-command (dev-master 9656de3)
Writing lock file
Generating autoload files
---
Success: Package installed.
[email protected]:~# wp package install wp-cli/profile-command --allow-root
Installing package wp-cli/profile-command (dev-master)
Updating /root/.wp-cli/packages/composer.json to require the package...
Using Composer to install the package...
---
Loading composer repositories with package information
Updating dependencies
Resolving dependencies through SAT
Dependency resolution completed in 0.142 seconds
Analyzed 5465 packages to resolve dependencies
Analyzed 333657 rules to resolve dependencies
Package operations: 0 installs, 0 updates, 0 removals
Writing lock file
Generating autoload files
---
Success: Package installed.

We can combine the two commands using && to update both WP-CLI’s core and its packages

wp cli update --yes --allow-root && wp package update --allow-root

Now we can add the cronjob, first open the crontab editor

crontab -e

Now add this line to your cron list to automatically run the command every day at midnight.

@daily wp cli update --yes --allow-root && wp package update --allow-root

Use Ctrl+X, Y and Enter to Save and Exit if the crontab editor uses nano.