检测系统环境、配置开机自启动
1.编写自检测脚本
话不多说,直接上代码
vi neofetch
#!/bin/bash
# 注意:该脚本适用于ubuntu20.04版本,其他linux版本可能不兼容
# Define ANSI color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RESET='\033[0m'
# Define functions to get system information
get_os_info() {
echo -e "${GREEN}Operating System:${RESET} $(lsb_release -d | cut -f 2)"
}
get_cpu_info() {
echo -e "${GREEN}CPU:${RESET} $(cat /proc/cpuinfo | grep "model name"|head -n 1|awk -F ': *' '{print $2}'
) "
}
get_gpu_info() {
echo -e "${GREEN}GPU:${RESET} $(lspci|grep -i vga|tail -n 1|awk -F ': *' '{print $2}')"
}
get_kernel_info(){
echo -e "${GREEN}内核:${RESET} $(uname -r)"
}
get_disk_info(){
#分区名称
disks=$( df -h | grep '/dev/sd[0-9a-z]\+[0-9]'|awk '{print $1,$3,$2,$6}')
OLD_IFS="$IFS"
IFS=$'\n'
disks=($disks)
disk_num=$(lsblk | grep -c disk)
echo -e "${GREEN}硬盘块数:${RESET} ${disk_num} 块"
##总容量
for disk in ${disks[@]}
do
IFS="$OLD_IFS"
tmp_disk=(${disk})
printf "分区名称:%-8s 挂载目录: %-12s 使用情况:%-3s / %-3s \n" "${tmp_disk[0]}" "${tmp_disk[3]}" "${tmp_disk[1]}" "${tmp_disk[2]}"
IFS=$'\n'
done
IFS="$OLD_IFS"
}
get_net_info(){
OLD_IFS="$IFS"
IFS=$'\n'
net_interfaces=($( ifconfig | grep -oE '^([a-z0-9]+):' | awk -F ":" '{print $1}') )
echo -e "${GREEN}网络接口数:${RESET}${#net_interfaces[@]}"
# 获取网络信息文本
net_info=$(ifconfig)
# 使用awk提取接口信息并分组打印
echo "$net_info" | awk '
/^[a-zA-Z0-9]+: / {
interface = $1;
getline;
while ($1 == "inet" || $1 == "inet6") {
if ($1 == "inet") {
inet = $2;
netmask = $4
} else if ($1 == "inet6") {
inet6 = $2;
}
getline;
}
print "Interface: " interface;
print "Inet4 Address: " inet;
print "Inet6 Address: " inet6;
print "Netmask: " netmask;
print "----------------------";
}
'
IFS="$OLD_IFS"
}
get_hostname(){
hostname=$(hostname)
echo -e "${GREEN}主机名:${RESET}${hostname}"
}
get_time(){
echo -e "${GREEN}检测时间:${RESET}`date`"
}
# Main function to display system information
show_system_info() {
echo -e "${GREEN}=============环境检测工具=============${RESET}"
echo -e "
xxx xxx xxx xxx
xxx xxx xxx xxx
xxxxxx xxx xxx
xxxx xxx xxx
xxxxxx xxx xxx
xxx xxx xxx xxx
xxx xxx xxxxxxx
${RESET}"
echo -e "${GREEN}=============环境检测工具=============${RESET}"
get_hostname
get_os_info
get_cpu_info
get_gpu_info
get_kernel_info
get_disk_info
get_net_info
get_time
}
# Call the main function to display system information
show_system_info > /var/log/self_neofetch.log 2>&1
因为代码中配置了输出重定向,所以查看/var/log/self_neofetch.log
cat /var/log/self_neofetch.log
2.编写开机自启动服务
/etc/systemd/system/下创建一个self_neofetch.service的文件,内容如右所示
sudo vi /etc/systemd/system/self_neofetch.service
#内容如下:
[Unit]
Description=配置的是开机自启动,检测主机相关配置信息
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/home/xubin/shell/neofetch #需要执行的脚本路径
#执行一次
Type=oneshot
[Install]
WantedBy=multi-user.target
3.开启自启动
#重新加载服务
sudo systemctl daemon-reload
#配置开机启动
sudo systemctl enable self_neofetch.service
sudo systemctl start self_neofetch.service
sudo systemctl status self_neofetch.service
4.重启,验证
重启系统
因为脚本的输出已经重定向到/var/log/self_neofetch.log中
只需查看其中的内容即可
cat /var/log/self_neofetch.log
发现此时的时间就是开机自动执行脚本的时间了,验证成功