Skip to content

Instantly share code, notes, and snippets.

@chmac
Created February 14, 2017 20:37
Show Gist options
  • Save chmac/7516d610616a27bf5913272470287096 to your computer and use it in GitHub Desktop.
Save chmac/7516d610616a27bf5913272470287096 to your computer and use it in GitHub Desktop.
Bash script to generate an email when security updates are pending on Ubuntu
#!/bin/bash
# Grab the pending updates like 4;2
updates=$(/usr/lib/update-notifier/apt-check 2>&1)
# If there are no pending updates, exit now
if [ $updates == "0;0" ]
then
exit
fi
# Get the number of security updates (after the colon)
security=$(echo "$updates" | cut -d ";" -f 2)
regular=$(echo "$updates" | cut -d ";" -f 1)
if [ $security != "0" ]
then
# Generate some output which will be piped to cron
summary=$(/usr/lib/update-notifier/apt-check --human-readable)
middle=$'\n\n'"Packages pending upgrade:"$'\n'
details=$((/usr/lib/update-notifier/apt-check --package-names) 2>&1)
message="$summary$middle$details"
echo "$message" | mail -s "$(hostname -s) $security security updates ($regular updates)" $(whoami)
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment