Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JonTheNiceGuy
Created June 5, 2020 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonTheNiceGuy/153c815aa2288b09abfcef7b6cd40578 to your computer and use it in GitHub Desktop.
Save JonTheNiceGuy/153c815aa2288b09abfcef7b6cd40578 to your computer and use it in GitHub Desktop.
A simple script to upgrade all wordpress components in cron

wp-update.sh

A simple tool to update and upgrade Wordpress components

A few years ago, I hosted my blog on Dreamhost. They've customized something inside the blog which means it doesn't automatically update itself. I've long since moved this blog off to my own hosting, but I can't figure out which thing it was they changed, and I've got so much content and stuff in here, I don't really want to mess with it.

Anyway, I like keeping my systems up to date, and I hate logging into a blog and finding updates are pending, so I wrote this script. It uses wp-cli which I have installed to /usr/local/bin/wp as per the install guide.

This updates:

  1. All core files (lines core update-db, core update and language core update)
  2. All plugins (lines plugin update --all and language plugin update --all)
  3. All themes (lines theme update --all and language theme update --all)

To remove any part of this script, just delete those lines, including the /usr/local/bin/wp and --quiet && \ fragments!

I then run sudo -u www-data crontab -e (replacing www-data with the real account name of the user who controls the blog, which can be found by doing an ls -l /var/www/html/ replacing the path to where your blog is located) and I add the bottom line to that crontab file (the rest is just comments to remind you what the fields are!

#                                         day of month [1-31]
#                                             month [1-12]
#                                                 day of week [1-6 Mon-Sat, 0/7 Sun]
# minute   hour                                         command
1          1,3,5,7,9,11,13,15,17,19,21,23 *   *   *     /usr/local/bin/wp-upgrade.sh /var/www/jon.sprig.gs/blog

This means that every other hour, at 1 minute past the hour, every day, every month, I run the update :)

If you've got email setup for this host, you'll get an email whenever it upgrades a component too.

#!/bin/bash
if [ -n "$1" ] && [ -e "$1" ]
then
cd "$1"
fi
/usr/local/bin/wp core update-db --quiet && \
/usr/local/bin/wp core update --quiet && \
/usr/local/bin/wp language core update --quiet && \
/usr/local/bin/wp plugin update --all --quiet && \
/usr/local/bin/wp language plugin update --all --quiet && \
/usr/local/bin/wp theme update --all --quiet && \
/usr/local/bin/wp language theme update --all --quiet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment