Loading

更换树莓派ssh欢迎信息显示温度CPU、内存硬盘占用率

背景

希望每次ssh登录树莓派时欢迎信息可以显示系统状态
包括CPU温度与用量,内存和硬盘用量

代码

控制树莓派登录欢迎信息的文件有两处

  • 位于 /etc/update-motd.d/目录下的shell脚本
  • 上述脚本执行完毕后打印/etc/motd文本
    新建欢迎脚本
$ sudo vi /etc/update-motd.d/11-info

部分源自Adafruit的开源项目修改而来(已停止维护)

#!/bin/sh
uptime | awk '{printf("\nCPU Load: %.2f\t", $(NF-2))}'
free -m | awk 'NR==2{printf("Mem: %s/%sMB %.2f%%\n", $3,$2,$3*100/$2)}'
cat /sys/class/thermal/thermal_zone0/temp|awk '{printf("CPU Temp: %.2f\t",$1/1000)}'
df -h | awk '$NF=="/"{printf "Disk: %.1f/%.1fGB %s\n\n", $3,$2,$5}'

添加执行权限

$ sudo chmod +x /etc/update-motd.d/11-info

取消原始静态欢迎信息(可选)

$ sudo mv /etc/motd /etc/motd.sample

效果

更换前

Linux rpi0w 4.19.57+ #1244 Thu Jul 4 18:42:50 BST 2019 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Nov 26 19:57:02 2020 from 127.0.0.1

更换后

Linux rpi0w 4.19.57+ #1244 Thu Jul 4 18:42:50 BST 2019 armv6l
CPU Load: 0.01	Mem: 74/432MB 17.13%
CPU Temp: 29.86	Disk: 2.0/7.1GB 30%
Last login: Thu Nov 26 19:57:02 2020 from 127.0.0.1

参考

adafruit/Adafruit_Python_SSD1306: Python library to use SSD1306-based 128x64 or 128x32 pixel OLED displays with a Raspberry Pi or Beaglebone Black.

posted @ 2020-11-27 23:46  azureology  阅读(983)  评论(0编辑  收藏  举报