FreeBSD system and ports update script

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:

4 Comments

  • #1 by Miklos on 5. September, 2011 - 16:50

    Reply Quote

    I updated the article as I made an error in the cronjob command 🙂

  • #2 by Mark Barthelemy on 17. September, 2015 - 20:34

    Reply Quote

    First off, thanks for the script! It has made my life so much easier!
    Quick question. I noticed today that I got the following in an email.
    Fetching 58 patches…..10….20….30….40….50…. done.
    Applying patches… done.

    The following files will be updated as part of updating to 10.2-RELEASE-p3:
    .
    .
    bunch of files
    .
    .
    Installing updates… done.

    My question is, do I need to run buildworld and installworld for all of this to take affect? I presume I do as uname -a still shows P2.

    Thanks,

    Mark

  • #3 by Miklos on 18. September, 2015 - 04:18

    Reply Quote

    Hi Mark,

    The patching is done via ‘freebsd-update’ which is binary updates, if there are kernel updates you will want to reboot else it’s already running when you get the email 🙂

  • #4 by Miklos on 18. September, 2015 - 04:20

    Reply Quote

    I should mention the patch level (-p3 etc.) is only updated when you reload the kernel (reboot), so it’s quite common to have uname show an older patchlevel if there has been no kernel updates since last update.

Page not found - Sweet Captcha
Error 404

It look like the page you're looking for doesn't exist, sorry

Search stories by typing keyword and hit enter to begin searching.