Custom Raspberry Pi MOTD (Fish)

2 minute read

In my weekly tinkering sessions I almost always end up in my Pi 3. The default MOTD on the Pi is a licence reminder which after a while becomes a bit boring. Time to spice it up by introducing one of my favourite things to do at 3am - unnecessary complexity.

Bash syntax ≠ Fish syntax

My last post talked about how I switched from Bash to Fish as my default shell (TL;DR it’s great). So finding a pre-made MOTD command wasn’t possible. I used a post on the raspberry pi forums as a reference but had to edit the syntax to suit Fish. My Pi is running headless in my cupboard and I wanted to include some of the basic information I would find useful.

  • Memory
  • Core temperature
  • Current uptime
  • Weather (so I have even less reason to open the curtains)

I actually learned a lot during this micro-project because most of these unix commands I was not familiar with. Plus it had me fiddling round with the Fish documentation, which I normally like to do anyway when using a new tool.

I discovered there are actually two places where the Fish MOTD can come from:

  1. Universal fish greeting
    • set --universal fish_greeting
  2. MOTD file
    • ~/.config/fish/config.fish

The MOTD runs first, then the universal greeting is ran also. You can pass variables from one to the other but I found this to be unreliable. The best way I found is just to keep everything inside the MOTD file.

Here’s what I put in there:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Variables
## Memory
set memFree (cat /proc/meminfo | grep MemFree | awk '{print $2 " kB Free"}')
set memTotal (cat /proc/meminfo | grep MemTotal | awk '{print $2 " kB Total"}')

## Uptime
set upTimeTotal (/usr/bin/cut -d. -f1 /proc/uptime)
set upTimeSecs (math "$upTimeTotal%60")
set upTimeMins (math "$upTimeTotal/60%60")
set upTimeHours (math "$upTimeTotal/3600%24")
set upTimeDays (math "$upTimeTotal/86400")
set whoAMI(whoami)

set upTime (printf "%d days, %02dh%02dm%02ds" "$upTimeDays" "$upTimeHours" "$upTimeMins" "$upTimeSecs")

## Weather
set piWeather (curl -s "http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=EUR|UK|UK001|LIVINGSTON|" | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3, \1/p')

## Core Temperature
set coreTemp (sudo vcgencmd measure_temp | sed 's/.*\(......\)/\1/')

echo \nWelcome, (whoami | sed 's/[^ _-]*/\u&/g')". How are you today?"
echo (set_color red)"Memory:    " $memFree "/" $memTotal
echo "Core Temp: " $coreTemp
echo "Uptime:    " $upTime
echo "Weather:   " $piWeather\n

The result is pleasing, though it adds a second of wait time to get to the command line.

Pi MOTD