Skip to content

Instantly share code, notes, and snippets.

@AdrianLThomas
Created April 13, 2020 07:46
Show Gist options
  • Save AdrianLThomas/b70ec5b3e977015ed20bba7a24e3bc26 to your computer and use it in GitHub Desktop.
Save AdrianLThomas/b70ec5b3e977015ed20bba7a24e3bc26 to your computer and use it in GitHub Desktop.
Auto update Ubuntu Server on Cron script
# This is a script made to keep your Ubuntu server up to
# date placing this script to /etc/cron.weekly/autoupdate.sh.
# This script updates your server automatically and informs
# you via email if the update was succesful or not.
# Set the variable $admin_email as your email address.
admin_mail="your.name@email.com"
# Create a temporary path in /tmp to write a temporary log
# file. No need to edit.
tmpfile=$(mktemp)
# Run the commands to update the system and write the log
# file at the same time.
echo "aptitupde update" >> ${tmpfile}
aptitude update >> ${tmpfile} 2>&1
echo "" >> ${tmpfile}
echo "aptitude full-upgrade" >> ${tmpfile}
aptitude -y full-upgrade >> ${tmpfile} 2>&1
echo "" >> ${tmpfile}
echo "aptitude clean" >> ${tmpfile}
aptitude clean >> ${tmpfile} 2>&1
# Send the temporary log via mail. The fact if the upgrade
# was succesful or not is written in the subject field.
if grep -q 'E: \|W: ' ${tmpfile} ; then
mail -s "Upgrade of your server failed $(date)" ${admin_mail} < ${tmpfile}
else
mail -s "Upgraded your server succesfully $(date)" ${admin_mail} < ${tmpfile}
fi
# Remove the temporary log file in temporary path.
rm -f ${tmpfile}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment