Creating a Dynamic MOTD

If like me, you use a Raspberry Pi remotely from SSH a lot, be it a web server or network monitor it is always handy to have a quick eye on the running of it. This guide will put some handy outputs in the MOTD (Message Of The Day) upon logging in.

pic1

The MOTD is commonly used to pass messages on to users of the server easily without the need to mail everyone. Presently you will have seen it when you log on but chances are never paid much attention to it;

pic2

First things

Please note, although we aren’t modifying the core system files, we are playing very close to them, so take your time and ensure there are no spelling mistakes before hitting enter! I can’t take responsibility for errors typed and files/file systems being deleted. You have been warned!

 

Typically the Message Of The Day is only generated on system boot, which is fine when getting the hostname, or kernel version as they require a reboot to take effect, but getting current CPU or memory stats will need a script to fetch these and store them ready to serve.

The file of interest is /var/run/motd.dynamic, but this is deleted and regenerated on boot, so firstly we need to create the script to write this file with our own information.

sudo nano /etc/init.d/updatemotd.sh

Information to display

Now the file can have any shell command of your choosing, but using this code is a good start; So copy and paste the following in.

#!/bin/sh

uname -snrvm > /var/run/motd.dynamic
echo "" >> /var/run/motd.dynamic
uptime | sed -e 's/[,].*//' -e 's: ::' -e 's/^.*up //' -e 's/^/Uptime: /' >> /var/run/motd.dynamic
echo "" >> /var/run/motd.dynamic
/opt/vc/bin/vcgencmd measure_temp | sed -r -e 's/^[^=]*=//' -e 's/^/CPU Temperature: /' >> /var/run/motd.dynamic
echo "" >> /var/run/motd.dynamic
free -o -h >> /var/run/motd.dynamic
echo "" >> /var/run/motd.dynamic
df -hT /home >> /var/run/motd.dynamic

pic3

A quick breakdown of what is included here;

Line 3: Get the hostname, followed by the kernel version

Linux raspberrypi 4.1.18-v7+ #846 SMP Thu Feb 25 14:22:53 GMT 2016 armv7l

Line 4 (and 6, and 8, and 10): Are there to add a line space between each command to make reading each output easier

echo "" >> /var/run/motd.dynamic

Line 5: Show the current system uptime, but strip out the current time at the beginning and user information at the end

Uptime: 20 min

Line 7: Display the CPU temperature, with formatting to make it look nicer

CPU Temperature: 31.5'C

Line 9: Display current RAM and Swap in a table format

         total  used    free  shared   buffers  cached
Mem:     925M   182M    743M  6.7M     13M      99M
Swap:    99M    0B      99M

Line 11: Show the amount of free space for the main filesystem

Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/root      ext4  7.3G  3.3G  3.7G  48% /

 

Making it executable

As you will notice, each line will save the output in to the file /var/run/motd.dynamic.

Save the file, by pressing Control + X, Y to confirm and Enter to write the file. Make the file executable to the system with;

sudo chmod +x /etc/init.d/updatemotd.sh

Running the file should hopefully bring up no errors, quickly test this with;

sudo /etc/init.d/updatemotd.sh

pic4

And display the output with;

cat /var/run/motd.dynamic

pic5

All good? If not double check the block of code above has the complete line, as lines 5 and 7 are quite long and may roll off the end of the screen.

Automating the information collection

For this, an update every minute I feel is enough, as once I have logged in, I can run a command to get up to date information that I need, and the output should only be a quick overview. Cron will be able to handle with ease updates every minute, or hourly, or weekly, so lets create an entry;

crontab -e

pic6

If this is the first time it has been run, you’ll be asked for your editor of choice (it will remember what you put!), as we have used nano throughout, press 2 and enter.

pic7

Scroll to the bottom, and type in;

* * * * * sudo /etc/init.d/updatemotd.sh

pic8

Each star signals when it should be run, the star meaning every. From left to write, this command is stating;

Every minute, every hour, every day, every month, and every day of week

Control + X, Y and Enter to save the file. When the screen returns it should state installing new crontab just to confirm it has saved.

pic9

A quick cat of the file will show the file updating

pic10

Putting in to action

This should be everything to make a quick dynamic MOTD, so all it needs now is to try it out. Open a new SSH window, and log in as normal.

pic11

Voila!