WordPress automatic updates – the hassle free way
By Vince
I have quite a few wordpress sites under management, and updating the plugins and themes can get quite tedious. This script will update all of them automatically (you will need SSH and CRON access for this) to whatever time-frame you set it to. The trick is to use “wp-cli” which is a command line toolset for WordPress that lets you do just about anything (you can even reset admin passwords so be careful who has access to this stuff).
I update all of the sites every day and have been doing this for years without any issues. I get a cron email every day telling me which plugins were updated and if anything happened.
Word of caution: there is potential that you can mess up your site, especially with these kinds of updates. I highly recommend you setup an external monitoring system like Nagios/Pingdom that will let you know if there is a site issue.
Step 1: If you are running on a Unix server, download the wp-cli.phan toolset from: wp-cli.org
You can install this file anywhere, just make sure it’s not internet accessible (keep it outside of /var/www typically)
cd /opt
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
See if it downloaded properly:
sudo -u www-data php5 wp-cli.phar --info
#OUTPUT
PHP binary: /usr/bin/php5
PHP version: 5.5.9-1ubuntu4.17
php.ini used: /etc/php5/cli/php.ini
WP-CLI root dir: phar://wp-cli.phar
WP-CLI packages dir: /xxx/.wp-cli/packages/
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 0.23.1</code>
Now we need to create a shell script that will do the updating for us (via a cron job).
#use your favorite editor and create a new file:
vi /opt/update_wordpress.sh
#PASTE THIS:
#!/bin/bash
#this line is to run any wordpress cron jobs you have
wget -q -O - http://www.example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
echo "Updating wordpress"
cd /var/www/html
php5 /opt/wp-cli.phar plugin update-all
php5 /opt/wp-cli.phar theme update-all
echo ""
echo "Done"
Now we need to make that file executable:
chmod +x /opt/update_wordpress.sh
Now let’s test it out!
sudo -u www-data /opt/update_wordpress.sh
Updating WordPress
Success: Updated 0/0 plugins.
Success: Updated 0/0 themes.
Done
Great! The last part is to add it to cron so this will run every night. We are going to use the www-data user and add this to their cron so it runs as the web user.
NOTE: this user might be different for you, if you use Nginx the username could be nginx. If you try to add a cron job to a non-existent user, it will tell you.
#open the cron editor
crontab -u www-data -e
#paste this at the bottom
30 2 * * * /opt/update_wordpress.sh
What this says is every night at 2:30AM run the update script as the www-data user (we don’t want to run this as root, it will break things)
That’s it, you can add as many blogs/directories as you like to the update_wordpress.sh file and it will update them as well.
One thing to note, you will get an email every night only if you have an email delivery program setup. I personally use Postfix since it’s very easy to setup. You will need to setup an email alias for this user.
If you have a custom theme this might not work properly, but it will error out when you test it.