在树莓派登录信息中加入温度 CPU、内存硬盘占用率信息

背景

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

代码

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

1、位于 /etc/update-motd.d/ 目录下的 shell 脚本
2、上述脚本执行完毕后打印 /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 128×64 or 128×32 pixel OLED displays with a Raspberry Pi or Beaglebone Black.

原文链接 https://www.cnblogs.com/azureology/p/14051040.html



2 评论

发表评论

你的邮件地址不会公开


*