Saturday, June 15, 2019

Updating the Message of the Day (motd) on Raspberry Pi

I got a Raspberry Pi for my birthday last year, and this year I’ve been really playing with it.  I mostly got it to because I want an easy place to run my Node.js scripts, and spend more time learning a Linux environment.  I’ve really been enjoying having a Pi in the house to play with, but the most impressive thing to me is the uptime of having a system that does so little.  Today started with me wondering why on my MacBook I open the terminal and the uptime is already present, but when I log into my pi, the uptime needs to be typed.
 
It was time to change that.
 
I quickly discovered by Googling a bit that this welcome message was called “The Message of the Day”, or “motd” for short. The raspberry pi website has a great forum post about how to customize this to something that I really wanted. The post is “Custom MOTD - Message of the Day”. Look how nice this logon message is: 
 
rasplogin.jpg
 
 
However, by following the instructions on this page I had one major problem.  My colour scheme for my command prompt disappeared. Additionally yanewby posts that there may be a better place to have this run.  Well I found that and I wanted to post my findings.
 

How to update the message of the day

My Pi is also in a headless conifguration, meaning I just SSH into it to use it.  So there are a number of files that matter here.  I ended up not using the ~/.bash_profile which the user specified in his post.  Instead there are a few other files
  • /etc/motd - This is a static file that is displayed.  If you just want a static message, type it in here, and it’ll be posted every time you log in
  • /etc/update-motd.d - this is a folder full of scripts to be run
  • /var/run/motd.dynamic - this appears to be output of the last run of the above
In the /etc/update-motd.d folder, there are a number of executible files.  All of these are run as long as they are marked as executible.  I had one called `10-unamed`. The files run in alphabetical order, so if you added an `01-myScript` it would run before 10-unamed, and if it was `11-myScript` it would run after 10-unamed. I ended up backing up this file to my ~ directory, and editing it.
 
If you do make a new file, make sure you make it executable with the following command:
chmod +x filename
I had to edit some of the script from the original user to get what I wanted. My changes included:
  1. I had to specify which shell to run it with, so my first line is "#!/bin/bash"
  2. I also realized that the terminal hadn’t been set up for colour, so to get colour I had to add another line “export TERM=xterm-256color"
  3. The rest of the script was updated to
    1. Put a space between the hr’s, min’s, & sec’s of uptime.
    2. Change when the colours switched
    3. add my device name to the uname command
    4. Supply my actual disk to get the free space
    5. change the code to get my LAN ip address
    6. Change the code to get my local weather (I don’t live in the UK!)
You can see my code here:
#!/bin/bash
export TERM=xterm-256color
let upSeconds="$(/usr/bin/cut -d. -f1 /proc/uptime)" 
let secs=$((${upSeconds}%60))
let mins=$((${upSeconds}/60%60))
let hours=$((${upSeconds}/3600%24))
let days=$((${upSeconds}/86400))
UPTIME=`printf "%d days, %02dh %02dm %02ds" "$days" "$hours" "$mins" "$secs"`
 
# get the load averages read one five fifteen
rest < /proc/loadavg
 
echo "$(tput setaf 2)
    .~~. .~~.             $(tput sgr0)`date +"%A, %e %B %Y, %r"`$(tput setaf 2)
  '. \ ' ' / .'           $(tput sgr0)`uname -snrmo`$(tput setaf 1)
   .~ .~~~..~.
  : .~.'~'.~. :           $(tput sgr0)Uptime.............: ${UPTIME}$(tput setaf 1)
   ~ ( ) ( ) ~            $(tput sgr0)Memory.............: `cat /proc/meminfo | grep MemFree | awk {'print $2'}`kB (Free) / `cat /proc/meminfo | grep MemTotal | awk {'print $2'}`kB (Total)$(tput setaf 1)
( : '~'.~.'~' : )         $(tput sgr0)Load Averages......: ${one}, ${five}, ${fifteen} (1, 5, 15 min)$(tput setaf 1)
  ~ .~ ( ) ~. ~           $(tput sgr0)Running Processes..: `ps ax | wc -l | tr -d " "`$(tput setaf 1)
   ( : '~' : )            $(tput sgr0)Free Disk Space....: `df -Pk | grep -E '^/dev/root' | awk '{ print $4 }'`k (`df -Pk | grep -E '^/dev/root' | awk '{ print $5 }'` used) on /dev/root$(tput setaf 1)
   '~ .~~~. ~'            $(tput sgr0)IP Addresses.......: LAN: `/sbin/ifconfig eth0 | /bin/grep "inet " | awk '{ print $2 }'` WAN: `wget -q -O - http://icanhazip.com/ | tail`$(tput setaf 1)
       '~'                $(tput sgr0)Victoria weather...: `curl -s "http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=EN|CA|VICTORIA" | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3, \1/p'`
 
 That’s all there is to it. Make sure it’s executable, and now disconenct and reconnect and you should see your new message of the day. The above produces this: