前言
手上有一台3B+按了LCD屏,平时又没什么用,打算用来开机后实时展示机器状态,so需要去掉免密登录+实时打印系统情况
效果如下
免密
sudo nano /etc/systemd/system/getty.target.wants/getty\@tty1.service
修改为,pi为用户名
#ExecStart=-/sbin/agetty -o '-p -- \\u' --noclear %I $TERM
ExecStart=-/sbin/agetty --autologin pi --noclear %I $TERM
开机IP及系统状态
我们知道,想要在系统启动时加入脚本一般在**/etc/rc.local**中修改,但是该加载顺序优先级比较高,导致和其他系统加载项一起刷让我们无法看到日志
如果想要再系统完全启动后再打印日志的话,我们需要了解Linux的开启启动顺序
自启动脚本实践
- 开机以 root 权限执行脚本,修改
/etc/rc.d/rc.local
文件。 - 用户登录时执行脚本,如设置一些环境变量,修改
/etc/profile
文件。 - 特定用户登录时执行特定脚本,如设置该用户特定的环境变量,修改
~/.bash_profile
文件。
文件 | 说明 |
---|---|
/etc/profile | 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行。并从 /etc/profile.d 目录的配置文件中搜集 shell 的设置。 |
/etc/bashrc | 为每一个运行bash shell的用户执行此文件。当bash shell被打开时,该文件被读取。 |
~/.bash_profile | 用户专用于自己使用的 shell 信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的 .bashrc 文件。 |
~/.bashrc | 该文件包含专用于你的 bash shell 的 bash 信息,当登录时以及每次打开新的 shell 时,该文件被读取。 |
~/.profile | 在 Debian 中使用 .profile 文件代替 .bash_profile 文件 .profile (由Bourne Shell和Korn Shell使用)和 .login (由C Shell使用)两个文件是 .bash_profile 的同义词,目的是为了兼容其它 Shell。 |
~/.bash_logout | 当每次退出系统(退出bash shell)时,执行该文件。 |
所以我这里在/etc/profile
文件中加入打印脚本
sudo nano /etc/profile
最后一行加入
python /home/pi/python/ip.py
watch -n 10 python /home/pi/python/sysInfo.py #开机刷新在屏幕上,10s一次
脚本
ip
#!/usr/bin/python
# -*- coding:utf-8 -*-
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
print("ip:"+ip)
sysInfo
该脚本打印系统内存,磁盘,温度等占用情况
#!/usr/bin/python
# -*- coding:utf-8 -*-
import socket
import os
# Return CPU temperature as a character string
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
# Return RAM information (unit=kb) in a list
# Index 0: total RAM
# Index 1: used RAM
# Index 2: free RAM
def getRAMinfo():
p = os.popen('free')
i = 0
while 1:
i = i + 1
line = p.readline()
if i==2:
return(line.split()[1:4])
# Return % of CPU used by user as a character string
def getCPUuse():
return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))
# Return information about disk space as a list (unit included)
# Index 0: total disk space
# Index 1: used disk space
# Index 2: remaining disk space
# Index 3: percentage of disk used
def getDiskSpace():
p = os.popen("df -h /")
i = 0
while 1:
i = i +1
line = p.readline()
if i==2:
return(line.split()[1:5])
def getIp():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip
# CPU informatiom
CPU_temp = getCPUtemperature()
CPU_usage = getCPUuse()
# RAM information
# Output is in kb, here I convert it in Mb for readability
RAM_stats = getRAMinfo()
RAM_total = round(int(RAM_stats[0]) / 1000,1)
RAM_used = round(int(RAM_stats[1]) / 1000,1)
RAM_free = round(int(RAM_stats[2]) / 1000,1)
# Disk information
DISK_stats = getDiskSpace()
DISK_total = DISK_stats[0]
DISK_used = DISK_stats[1]
DISK_perc = DISK_stats[3]
if __name__ == '__main__':
print('IP = '+getIp())
print('')
print('CPU Temperature = '+CPU_temp)
print('CPU Use = '+CPU_usage)
print('')
print('RAM Total = '+str(RAM_total)+' MB')
print('RAM Used = '+str(RAM_used)+' MB')
print('RAM Free = '+str(RAM_free)+' MB')
print('')
print('DISK Total Space = '+str(DISK_total)+'B')
print('DISK Used Space = '+str(DISK_used)+'B')
print('DISK Used Percentage = '+str(DISK_perc))
参考文章