Tech Tip: Periodically Update Your MOTD with update-motd

 in

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.

motd-1.png

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:

motd-2.png

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.

AttachmentSize
motd-1.png4.4 KB
motd-2.png8.2 KB
______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

$ who -b|awk '{print $3 " "

Anonymous's picture

$ who -b|awk '{print $3 " " $4}'
2009-12-23 08:15

... looks cleaner to me.

hi, who is the developer of

T-One's picture

hi,

who is the developer of that stuff?
i googled for it but i cant find any development-portal or any infos about the devolpers in the web, just some ubuntu related stuff.

i want to port that stuff for redhat and fedora but i dont want to "fork" it just from some install packages. cvs? anyone?

thanks

anton

Developer

Mitch Frazier's picture

Check the man page it has the developer's name and email address.

Mitch Frazier is an Associate Editor for Linux Journal.

Nice. Except that you don't

Anonymous's picture

Nice. Except that you don't need update-modt to get there. I've been doing something similar for years simply putting all that in rc.local and generating the motd on the fly from header and various parts (just like my .sig).
Also remember motd is for people login in text mode only. for those loging directly into X you need xmessage or better still notification daemon (or use issue, built on the fly also).
Cheers

script problem...

Anonymous's picture

Not giving the btime? When I ran this script (just from a bash script),

#!/bin/bash
...
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
...

I get the following output...

       =======================================
       =         Welcome to FSERV1.          =
       =======================================

   ==== BOOT TIME ====           ==== UPTIME ====
                            >59 min

===================== DISK USAGE ======================
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             192G   71G  112G  39% /

================== CURRENTLY ONLINE ===================
NAME     LINE         TIME           IP ADDRESS

Boot Time

Mitch Frazier's picture

When I run "who -b" I get the following output:

     system boot  2008-12-04 01:24

The following would extract the correct data:

  btime=`who -b | sed -e 's/[\t a-zA-Z]*//'`

Or you could use:

  btime=`who -b | sed -e 's/.*system boot[ \t]*//'`

Mitch Frazier is an Associate Editor for Linux Journal.

Anonymous: What distribution

Bird's picture

Anonymous:

What distribution are you using? You should no doubt get output when typing "who -b" in the command line. Try running the the command in a console without assigning it to a variable and see what your output is. As stated, the output may vary from system to system and you will have to modify it if needed. Let us know what the output is.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix