LittleBizzy

Dominate local SEO with a regional SlickStack cloud server for just $39/month!  Order Now
LittleBizzy
Technical SEO & Cloud Server Experts

WordPress Plugins Automatic Update Shell Script

Updating WordPress plugins can be a HUGE hassle. Indeed, most webmasters are only aware of their ability to manually click the “Update” link next to various plugins in their WP Admin control panel, and aren’t even aware of other more automated options such as the free Easy Updates Manager plugin, among others. But there’s an even more direct option that’s “higher up” the server stack, which calls for a Linux shell script to update your WordPress plugins via a cron job.

Now, in most cases, this method is probably overkill. But there are times when it is the perfect solution, such as when you need to make sure a client or website always has the latest version of a plugin installed, say, via the Must-Use plugin directory (which hides the ability to deactivate or delete said plugins from your client).

WARNING: Before trying this script, make sure that you don’t already have the below plugins (etc) installed to your regular /plugins/ directory as the tutorial below is installing plugins to the /mu-plugins/ directory (“Must Use” Plugins) which could conflict with a plugin of the same name or function already existing elsewhere. Also keep in mind that several types of WordPress plugins will NOT function properly when installed as Must-Use (a.k.a. Multi-User) plugins.

Note: you must have root access to your server in order to properly configure this cron job script.

First, let’s create a new file i.e. muplugs-cron in which our bash script is going to reside:

cd /home/example
sudo touch /home/example/muplugs-cron
sudo nano /home/example/muplugs-cron

Copy and paste the below script into your newly created file (change username accordingly):

Since we are copying plugin files to our mu-plugins (or plugins) directory, there is no reason to preserve mode (permissions), ownership, or otherwise. The only attributes you may need to preserve are timestamps and symlinks, and even that is debatable. We use the -d flag to tell our script to preserve symlinks while not “following” them during the copy process.

Important: notice that sudo is not necessary as we will be adding this shell script to the root user’s crontab. Including sudo in a shell script requires a user’s password to be stored in plaintext = very bad idea! Also, keep in mind that unzip is not installed by default on most Linux servers so be sure that your server has that properly installed before activating this script.

Save variables in separate config file below:

#!/bin/bash
user="example"

Code last updated 6 August, 2016

#!/bin/bash
source config
cd /tmp/
wget --timestamping http://mirrors.littlebizzy.com/mu-plugins/autoloader.txt
wget --timestamping http://mirrors.littlebizzy.com/mu-plugins/wp-password-bcrypt.txt
wget --timestamping http://mirrors.littlebizzy.com/mu-plugins/better-search-replace.zip
wget --timestamping http://mirrors.littlebizzy.com/mu-plugins/cloudflare.zip
wget --timestamping http://mirrors.littlebizzy.com/mu-plugins/disable-embeds.zip
wget --timestamping http://mirrors.littlebizzy.com/mu-plugins/disable-emojis.zip
wget --timestamping http://mirrors.littlebizzy.com/mu-plugins/disable-xml-rpc.zip
wget --timestamping http://mirrors.littlebizzy.com/mu-plugins/error-log-monitor.zip
wget --timestamping http://mirrors.littlebizzy.com/mu-plugins/opcache.zip
wget --timestamping http://mirrors.littlebizzy.com/mu-plugins/revision-control.zip
wget --timestamping http://mirrors.littlebizzy.com/mu-plugins/stops-core-theme-and-plugin-updates.zip
wget --timestamping http://mirrors.littlebizzy.com/mu-plugins/wpstagecoach.zip
mv autoloader.txt autoloader.php
mv wp-password-bcrypt.txt wp-password-bcrypt.php
unzip /tmp/better-search-replace.zip
unzip /tmp/cloudflare.zip
unzip /tmp/disable-embeds.zip
unzip /tmp/disable-emojis.zip
unzip /tmp/disable-xml-rpc.zip
unzip /tmp/error-log-monitor.zip
unzip /tmp/opcache.zip
unzip /tmp/revision-control.zip
unzip /tmp/stops-core-theme-and-plugin-updates.zip
unzip /tmp/wpstagecoach.zip
rm -R /home/$user/www/wp-content/mu-plugins/better-search-replace
rm -R /home/$user/www/wp-content/mu-plugins/cloudflare
rm -R /home/$user/www/wp-content/mu-plugins/disable-embeds
rm -R /home/$user/www/wp-content/mu-plugins/disable-emojis
rm -R /home/$user/www/wp-content/mu-plugins/disable-xml-rpc
rm -R /home/$user/www/wp-content/mu-plugins/error-log-monitor
rm -R /home/$user/www/wp-content/mu-plugins/opcache
rm -R /home/$user/www/wp-content/mu-plugins/revision-control
rm -R /home/$user/www/wp-content/mu-plugins/stops-core-theme-and-plugin-updates
rm -R /home/$user/www/wp-content/mu-plugins/wpstagecoach
cp -R -d --remove-destination --preserve=timestamps --no-preserve=mode,ownership /tmp/autoloader.php /home/$user/www/wp-content/mu-plugins/autoloader.php
cp -R -d --remove-destination --preserve=timestamps --no-preserve=mode,ownership /tmp/wp-password-bcrypt.php /home/$user/www/wp-content/mu-plugins/wp-password-bcrypt.php
cp -R -d --remove-destination --preserve=timestamps --no-preserve=mode,ownership /tmp/better-search-replace /home/$user/www/wp-content/mu-plugins/better-search-replace
cp -R -d --remove-destination --preserve=timestamps --no-preserve=mode,ownership /tmp/cloudflare /home/$user/www/wp-content/mu-plugins/cloudflare
cp -R -d --remove-destination --preserve=timestamps --no-preserve=mode,ownership /tmp/disable-embeds /home/$user/www/wp-content/mu-plugins/disable-embeds
cp -R -d --remove-destination --preserve=timestamps --no-preserve=mode,ownership /tmp/disable-emojis /home/$user/www/wp-content/mu-plugins/disable-emojis
cp -R -d --remove-destination --preserve=timestamps --no-preserve=mode,ownership /tmp/disable-xml-rpc /home/$user/www/wp-content/mu-plugins/disable-xml-rpc
cp -R -d --remove-destination --preserve=timestamps --no-preserve=mode,ownership /tmp/error-log-monitor /home/$user/www/wp-content/mu-plugins/error-log-monitor
cp -R -d --remove-destination --preserve=timestamps --no-preserve=mode,ownership /tmp/opcache /home/$user/www/wp-content/mu-plugins/opcache
cp -R -d --remove-destination --preserve=timestamps --no-preserve=mode,ownership /tmp/revision-control /home/$user/www/wp-content/mu-plugins/revision-control
cp -R -d --remove-destination --preserve=timestamps --no-preserve=mode,ownership /tmp/stops-core-theme-and-plugin-updates /home/$user/www/wp-content/mu-plugins/stops-core-theme-and-plugin-updates
cp -R -d --remove-destination --preserve=timestamps --no-preserve=mode,ownership /tmp/wpstagecoach /home/$user/www/wp-content/mu-plugins/wpstagecoach
rm -R -f /tmp/autoloader.php
rm -R -f /tmp/wp-password-bcrypt.php
rm -R -f /tmp/better-search-replace /tmp/better-search-replace.zip
rm -R -f /tmp/cloudflare /tmp/cloudflare.zip
rm -R -f /tmp/disable-embeds /tmp/disable-embeds.zip
rm -R -f /tmp/disable-emojis /tmp/disable-emojis.zip
rm -R -f /tmp/disable-xml-rpc /tmp/disable-xml-rpc.zip
rm -R -f /tmp/error-log-monitor /tmp/error-log-monitor.zip
rm -R -f /tmp/opcache /tmp/opcache.zip
rm -R -f /tmp/revision-control /tmp/revision-control.zip
rm -R -f /tmp/stops-core-theme-and-plugin-updates /tmp/stops-core-theme-and-plugin-updates.zip
rm -R -f /tmp/wpstagecoach /tmp/wpstagecoach.zip
source /home/$user/perms

Finish up the script by making sure its executable and owned by the root user:

sudo chmod +x /home/example/muplugs-cron
sudo chown root:root /home/example/muplugs-cron

You will notice that our script file is merely named "muplugins" without any file extension, such as .sh or otherwise. This is because we’ve already made the script executable and included a "shebang" line at the top of our script to clarify to the root user which type of script this is (bash), so there is no need for a file extension type.

Lastly, we need to add this script to the root’s crontab file for scheduling:

sudo crontab -e

At the bottom of the crontab file, paste the below code (change username). /dev/null 2>&1 will prevent any cron reporting attempts being sent via email, which is best in the case that your VPS does not have an active mail server. The below cron job is scheduled to run once every 24 hours, which should be more than enough for most plugins:

0 0 * * * /home/example/muplugs-cron > /dev/null 2>&1

For good measure, test and then restart the Nginx service and then check that the script is working:

sudo nginx -t
sudo service nginx restart
About the Author

Jesse

Leave a Reply

Your email address will not be published. Required fields are marked *