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

Tony 219m2021-08-191411 次点击1 人收藏2 人感谢
https://www.cnblogs.com/azureology/p/14051040.html
## 背景
希望每次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
```
![1556e0e3b0cc4938c04244379cea8fbd.png](https://ppmm.org/images/2021/08/19/1556e0e3b0cc4938c04244379cea8fbd.png)
![0717d00fefeb28472b504c52201357a2.png](https://ppmm.org/images/2021/08/19/0717d00fefeb28472b504c52201357a2.png)
**********
[Linux的/etc/issue、/etc/issue.net和/etc/motd的区别](https://www.cnblogs.com/pluse/p/5531523.html)
收藏 ♥ 感谢
Spoony 小组长 2021-08-20 
这个很实用,我转载一下
九年吃菜粥 38.85m 2021-08-20 
早年看鸟哥私房菜时玩过,本质上就是拿输出做格式化字符串,现在好玩的是在shell里面调python,用python实现播放音频,更华丽的字符串输出等操作

登录注册 后可回复。




› 相关内容关注微信公众号