Big brass balls cron job

Category: 
Last modified: 
Friday, December 11, 2015 - 08:18
Author: 
Dan Eveland
Dan Eveland's picture

So, I like my sites to be up to date. For my personal projects, where I won't get fired when they stop running, I run cutting-edge stuff.

The following shell script I run every day at 1:00am will keep my install of Ubuntu Server at the most recent, and lean (no old kernels, for example). It also runs drush up and several other drush commands, such as optimizing database tables and truncating one particularly troublesome table.

I also have it clear out my Linux mail file because I don't really care about anything in there from the day before, and it will be re-created as needed anyway.

Keep in mind I run about 20 sites as a single Drupal multisite, so all those @sites commands run on each separate site in sequence.

At the end, it sends me a pushover alert that the script completed. If something goes very wrong, I would expect that not to send, and a separate site monitoring service will begin to alert me all my sites are offline.

I have been running variations on this script for 5 years and only once did it blow everything up. But... that's what backups are for!

Why do this? Well, for one thing, it keeps my sites and operating system completely patched at all times. Security patches are applied every single day, so vulnerabilities are mitigated very quickly. Also, when stuff does blow up, it's a great learning experience for my day job.

In general, the reliability and stability has proven to me that open source software is of great quality and the fear of updating is (largely) unfounded these days.

On to the shell script:

rm /var/mail/root

apt-get autoremove -y
apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-z]+//')") -y

cd /var/www/sites/daneveland.com
drush @sites rr -y
drush sql-query "TRUNCATE TABLE cache_update"
drush @sites en update dblog -y
drush @sites en optimizedb -y
drush @sites optimizedb-cleaning -y
drush @sites optimizedb-optimize -y
drush @sites cc all -y
drush @sites cron -y
drush up -y
drush @sites updatedb -y
drush @sites rr -y
drush @sites dis update dblog -y

apt-get update
apt-get upgrade -y
apt-get dist-upgrade -y

pear update-channels
pear upgrade

composer selfupdate
composer global update

curl -s \
  --form-string "token=xxx" \
  --form-string "user=xxx" \
  --form-string "message=Daily cron task on Server complete." \
  https://api.pushover.net/1/messages.json

reboot now