I thought I would share the script I use to keep track of port updates across all my FreeBSD servers. As usual know your basic FreeBSD (I assume that you have already use screen, portsnap and portmaster) to use this guide.

Put this script in /usr/local/sbin/daily.sh (and change the email).

#!/bin/sh

# Microsux.dk

PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin"
EMAIL="YOUR@EMAIL"
PORTOUTPUT=""

UNAME=`uname -ns`

SUBJECT="$UNAME updates available"
echo -e "Good morning,\n\n" > /tmp/dailymail.out

portsnap fetch update > /dev/null 2>&1
freebsd-update fetch > /tmp/dailyup.out

FUCHECK=`cat /tmp/dailyup.out|tail -1|awk '{ print $1 }'`

if [ "$FUCHECK" = "No" ]; then
  PORTOUTPUT=`portmaster -L|grep New`
  if [ "$PORTOUTPUT" != "" ]; then
	echo -e "Port updates available:\n" >> /tmp/dailymail.out
	portmaster -L|grep New >> /tmp/dailymail.out
	echo -e "\n\nEnjoy!\n" >> /tmp/dailymail.out
	mail -s "$SUBJECT" "$EMAIL" < /tmp/dailymail.out
  fi
else
  echo -e "FreeBSD updates fetched and installed:\n\n" >> /tmp/dailymail.out
  cat /tmp/dailyup.out >> /tmp/dailymail.out
  freebsd-update install >> /tmp/dailymail.out
  PORTOUTPUT=`portmaster -L|grep New`
  if [ "$PORTOUTPUT" != "" ]; then
	echo -e "\n\nPort updates available:\n" >> /tmp/dailymail.out
	portmaster -L|grep New >> /tmp/dailymail.out
	echo -e "\n\nEnjoy!\n" >> /tmp/dailymail.out
	mail -s "$SUBJECT" "$EMAIL" < /tmp/dailymail.out
  else
	echo -e "\n\nEnjoy!\n" >> /tmp/dailymail.out
	mail -s "$SUBJECT" "$EMAIL" < /tmp/dailymail.out
  fi
fi
sleep 5

Add the script to root’s crontab (change the time to avoid swamping the portsnap servers)

0 2 * * * /usr/local/bin/screen -d -m -S daily /usr/local/sbin/daily.sh

I prefer to run my script at 2am where I know there are almost no utilization on my servers – then when you wake up in the morning if there are port or system updates you will have a nice email (pr. server).

Enjoy!

Did you like this? Share it: