Script that notifies via email when your server has updates

If you are the administrator of a server then you definitely understand the importance of keeping your system updated always.

The following script will send you a mail whenever your server needs an update. The content of the mail will list out all the available updates.

The script

[code lang=”shell”]
#!/bin/bash
# Author: AppleGrew
# Site: www.applegrew.com
# License: GPL v2

yum check-update > /tmp/checkupdate.log
if [[ $? == 100 ]]
then
mail -s "New YUM updates available" -r admin@example.com $MAILTO < /tmp/checkupdate.log
fi
[/code]

Notes:

  • This script has been tested on CentOS 6 but should run on all RedHat based systems.
  • In the above script, change admin@example.com to any valid email id which your server is authorized to send. This the email id for the ‘From’ field. For example you can not set this to (say) someone@gmail.com. Since Google did not authorize your server to send emails on behalf of gmail.com.

Installation notes

  • Copy this script to some directory in your server and give it execute permission.
  • Now schedule a cron job for this script. You can set this job for any user, since this script does not need root privileges.
  • In the cronjob make sure to set the MAILTO environment variable. The script uses this variable to determine the recipient of the notification.

Example cronjob

[code lang=”plain”]
MAILTO=yourmail@email.com
0 0 * * * /home/user1/checkupdate.sh
[/code]

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.