Tech Tip: Periodically Update Your MOTD with update-motd
This tech tip provides you with information on how to customize your motd (Message Of The Day) message to display the output of one or more scripts. This uses the update-motd package, which updates the motd message when run. I use this method on Ubuntu 9.10, but not all systems provide this package so extra effort may be required to use it on other systems.
The update-motd daemon can be installed with:
$ sudo apt-get install update-motd
How update-motd operates depends on the version of update-motd that you have installed but the net effect is that it runs a set of scripts whose output is concatenated to produce a new motd message.
If you navigate to /etc/update-motd.d/ you will notice that there are a series of pre-defined scripts that begin with numbers and a hyphen. The numbers tell update-motd which script to run first (from lowest to highest) and in what order to display them on the output. If you’d like, you can either create a new script or add your commands to one of the existing scripts.
In my case, the script that displays my information is "10-stats", it contains:
#!/bin/bash
echo " ======================================="
echo " = Welcome to FSERV1. ="
echo " ======================================="
echo
btime=`who -b | sed -e 's/[^A-Z]*//'`
utime=`uptime | sed -e 's/ [0-9:]* up />/' -e 's/,.*//'`
echo " ==== BOOT TIME ==== ==== UPTIME ===="
echo " "$btime" " $utime
echo
echo "===================== DISK USAGE ======================"
df -h | egrep '(Filesystem)|(/dev/sd)'
echo
echo "================== CURRENTLY ONLINE ==================="
echo "NAME LINE TIME IP ADDRESS"
who –ips
In my case I’ve chosen to have the script display the date and time the system was booted (using who -b), the system uptime, disk usage information, and who is currently logged onto the system. To get the output as I want it I use sed to extract the pieces I want to output. The sed command is used to edit text, this does require some basic knowledge of regular expressions, a subject that could and has filled volumes.
The system uptime is derived from the command line:
utime=`uptime | sed -e 's/ [0-9:]* up />/' -e 's/,.*//'`
The full output from uptime would be something like:
10:19:47 up 375 days, 9:20, 4 users, load average: 0.93, 0.90, 0.81
I use sed to narrow it down to something like ">32 days". The -e specifies a sed "script" to use, I specify two scripts:
- s/ [0-9:]* up />/ - This replaces any repeated sequence of the characters "0 through 9 or a colon" ending in " up " with ">".
- s/,.*// - This replaces the first instance of a comma and any and all characters after it with a blank string (ie it wipes it out).
Note that the exact format of the output from uptime can differ across systems so this command could require some modifications.
When you’re done with your script, make sure to make it executable with chmod +x, now when you log in you should see your new motd message. Here's the output from my 10-stats script:
The hard drive stats I think are most critical. Every time I log in I can see where I stand with free space and migrate my data when necessary.
As I mentioned I use this method on Ubuntu 9.10, but not all systems provide the update-motd package and the version of the package you use can also affect the details of its operation, but the basic feature is the same: it gives you the ability to periodically change your motd message.