less wp-cli-a-simple-shell-script.md
wp-cli: a simple shell script
I wrote a very simple script to update a bunch of WordPress sites I have without having to log into the admin page every single day.
I take advantage of both wp-cli (a binary capable of performing countless administration functions on WordPress sites from the command line) and crontab.
Installing wp-cli is very easy and all you need to do is follow the installation instructions available at the following link: wp-cli installing
At this point, here is the format of the actual script:
#!/bin/bash
# file name update_wp_sites
# find directory owner: stat -c '%U' /var/www/${domain_name}/web
DOMAINS=`find /var/www -maxdepth 1 -type l | cut -d / -f4`
echo $DOMAINS
for domain in $DOMAINS
if [ -f /var/www/${domain}/web/wp-config.php]; then
do
USER=`stat -c '%U' /var/www/${domain}/web`
echo ${domain}
cd /var/www/${domain}/web
sudo -u $USER -- wp --path=/var/www/${domain}/web core update
sudo -u $USER -- wp --path=/var/www/${domain}/web core update-db --network
sudo -u $USER -- wp --path=/var/www/${domain}/web plugin update --all
sudo -u $USER -- wp --path=/var/www/${domain}/web theme update --all
done
fi
Many of you will have noticed that it works well with Ispconfig.
At this point, all we need to do is have crontab call the file daily at 2 in the morning
0 2 * * * /usr/local/sbin/update_wp_sites > /dev/null 2>&1