[工具] Shell 检测性能指标 (例如:剩余硬盘空间、内存空间等)

介绍

基本信息

名称: 检测性能指标(例如:剩余硬盘空间、内存空间等)
作用: 检测性能指标(例如:剩余硬盘空间、内存空间等)

使用方法

1. 给此脚本添加执行权限
2. 执行此脚本

脚本

#!/bin/bash

ip=`ip a s | awk '/[1-2]?[0-9]{0,2}\.[1-2]?[0-9]{0,2}/&&!/127.0.0.1/{print $2}'`
host=`hostname`
disk=`df -h | awk '/\/$/{print $5}'`
mem=`free -m | awk '/Mem/{print $4}'`
cpu=`top -bn 1 | awk -F',' '/^%Cpu/{print $4 }' | awk '{print $1}'`
soft=`rpm -qa | wc -l`
port=`ss -ntulap | wc -l`

echo "$ip $host disk $disk"
echo "$ip $host mem $mem"
echo "$ip $host cpu $cpu"
echo "$ip $host soft $soft"
echo "$ip $host port $port"
echo

[步骤] Linux 开机自启 (通过 chkconfig 实现)

案例一:添加一个受 chkconfig 管理的服务(脚本)

1.1 编写一个脚本

# vim /etc/init.d/start.sh

创建以下内容:

#!/bin/bash
systemctl start httpd

# chkconfig: 345 85 15
# description: This is a script of starting httpd

(补充:chkconfig:后面的 3 个含义为 httpd 的级别为 3、4 和 5,启动序号为 85,关闭序号为 15)

1.2 给脚本添加执行权限

# chmod +x /etc/init.d/start.sh

1.3 将脚本添加到 chkconfig 中

# chkconfig --add start.sh

1.4 显示刚刚添加到 chkconfig 的应用

# chkconfig --list

案例二:通过 chkconfig 管理一个服务或脚本

2.1 设定 start.sh 在 3 和 5 等级为 on

# chkconfig --level 35 start.sh on

2.2 设定 start.sh 在各等级为 on,“各等级”包括 2、3、4、5 等级

# chkconfig start.sh on

2.3 设定 start.sh 在各等级为 off,“各等级”包括 2、3、4、5 等级

# chkconfig start.sh off