Hello all,
Allow me to share some useful scriptlet with you.
It's something I find has been lacking for years for the average self-hosted FreeBSD user like me who occasionally logs in to his server to check if everything's going well, perform package upgrades etc, and wants to stay up to date with the latest version available of the operating system. Yet AFAIK (unless I missed something in the documentation for all these years, which is totally possible) there's no simple way for the system to tell you "hey there, a new production version is available, you should start considering upgrading" right at login. I know I could subscribe to a mailing list to be notified directly in my mailbox, but these emails rarely come at a moment where I'm ready to address this issue, and I'd rather be notified of that when I login to my server - because that's exactly when I'm available to perform routine maintenance tasks.
So in order to address this issue I've been using these few lines of shell script, which I put at the end of my ~/.profile.
Basically what it does, it simply fetches the www.freebsd.org website index, and looks at the number that's right after "Production:" in the middle of the page that tells the visitor which is the latest supported production release. Then it does a second query to cgit.freebsd.org to find out the current patchlevel for this version out of the sys/conf/newvers.sh script. It then assembles that data in a variable, and compares it with the current version string that the "freebsd-version" command returns. If the major/minor part differs, it suggests an OS upgrade. If the patchlevel differs, it tells you a security update is available. And if an error occured while trying to retrieve the latest available version tag, it tells you where it happened.
For example when I logged in today on my server, I got that line printed just before the shell (in glorious ANSI colors):
Should my system had been up to date, I'd have received that line instead:
I totally admit it's not the orthodox way of being informed - mailing lists are here for this purpose - but it nonetheless answered a need that I had, which was to obtain that information at the moment I'm focused on maintenance, and not in my mailbox among the mass of mail that one can receive daily.
I hope some of you will find it useful. Feel free to improve it.
Allow me to share some useful scriptlet with you.
It's something I find has been lacking for years for the average self-hosted FreeBSD user like me who occasionally logs in to his server to check if everything's going well, perform package upgrades etc, and wants to stay up to date with the latest version available of the operating system. Yet AFAIK (unless I missed something in the documentation for all these years, which is totally possible) there's no simple way for the system to tell you "hey there, a new production version is available, you should start considering upgrading" right at login. I know I could subscribe to a mailing list to be notified directly in my mailbox, but these emails rarely come at a moment where I'm ready to address this issue, and I'd rather be notified of that when I login to my server - because that's exactly when I'm available to perform routine maintenance tasks.
So in order to address this issue I've been using these few lines of shell script, which I put at the end of my ~/.profile.
Code:
# let's do an automatic update check (use http:// as long as it's supported since it's lighter, but note that https:// also works)
echo ""
echo -ne " Checking for operating system updates, please wait... "
_CUR_MMN="$(freebsd-version|awk -F- '{print $1}')"
_CUR_VER="$(freebsd-version)"
_AVL_MMN="$(HTTP_TIMEOUT=3 fetch -qo - "http://www.freebsd.org" 2>/dev/null|tr -d '\n'|awk -F"Production:" '{print $2}'|awk -F'>' '{print $2}'|awk -F'<' '{print $1}')"
_AVL_VER="${_AVL_MMN}-$(HTTP_TIMEOUT=3 fetch -qo - "http://cgit.freebsd.org/src/plain/sys/conf/newvers.sh?h=releng/${_AVL_MMN}" 2>/dev/null|grep "^BRANCH="|cut -d'"' -f2)"
if [ "_${_AVL_MMN}" = "_" ]; then
echo -e "\r /!\\ Automatic update check failed (error retrieving the latest production version from www.freebsd.org)."
elif [ "_$(echo "${_AVL_VER}"|grep "RELEASE")" = "_" ]; then
echo -e "\r /!\\ Automatic update check failed (error retrieving the latest patchlevel version from cgit.freebsd.org)."
elif [ "_${_CUR_VER}" = "_${_AVL_VER}" ]; then
echo -e "\r Your base system is up to date. Congratulations! "
elif [ ! "_${_CUR_MMN}" = "_${_AVL_MMN}" ]; then
echo -e "\r /!\\ A new FreeBSD version is available: \e[1;37m${_AVL_VER}\e[0m. Consider using \e[33mfreebsd-update\e[0m(8)."
else
echo -e "\r /!\\ A security update is available: \e[1;37m${_AVL_VER}\e[0m. Consider using \e[33mfreebsd-update\e[0m(8)."
fi
unset _AVL_VER _AVL_MMN _CUR_VER _CUR_MMN
echo ""
Basically what it does, it simply fetches the www.freebsd.org website index, and looks at the number that's right after "Production:" in the middle of the page that tells the visitor which is the latest supported production release. Then it does a second query to cgit.freebsd.org to find out the current patchlevel for this version out of the sys/conf/newvers.sh script. It then assembles that data in a variable, and compares it with the current version string that the "freebsd-version" command returns. If the major/minor part differs, it suggests an OS upgrade. If the patchlevel differs, it tells you a security update is available. And if an error occured while trying to retrieve the latest available version tag, it tells you where it happened.
For example when I logged in today on my server, I got that line printed just before the shell (in glorious ANSI colors):
/!\ A security update is available: 13.0-RELEASE-p11. Consider using freebsd-update(8).
Should my system had been up to date, I'd have received that line instead:
Your base system is up to date. Congratulations!
I totally admit it's not the orthodox way of being informed - mailing lists are here for this purpose - but it nonetheless answered a need that I had, which was to obtain that information at the moment I'm focused on maintenance, and not in my mailbox among the mass of mail that one can receive daily.
I hope some of you will find it useful. Feel free to improve it.
Last edited: