16

实时显示 VPS 资源占用

27 Jun 2023

把下面的脚本保存为status.sh,建议放到网站根目录下,脚本会保存占用数据到脚本虽在目录。

#!/bin/bash

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

while true; do
    # 获取CPU占用百分比
    cpu_usage=$(grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}')

    # 获取内存占用百分比
    mem_usage=$(free -m | awk '/Mem/ {usage = $3 / $2 * 100; printf "%.2f%", usage}')

    # 获取根目录占用百分比
    disk_usage=$(df -h / | awk '/\// {usage = $(NF-1); print usage}')

    # 构建JSON对象
    json='{ "cpu_usage": "'$cpu_usage'", "mem_usage": "'$mem_usage'", "disk_usage": "'$disk_usage'" }'

    # 将JSON对象写入文件
    echo "$json" > "$script_dir/system_usage.json"

    sleep 1
done

然后增加一个开机启动项,或者手动执行下面的命令

no /path/to/status.sh > /dev/null &

然后你就可以通过https://你的域名/system_usage.json获取到占用数据,比如32mb.cc的实时占用数据地址是https://32mb.cc/usr/themes/blackleft/status/system_usage.json
至于前端怎么写,问 ChatGPT 最快

    分类:默认分类 ; 标签:VPS , 实时占用
    叶开 December 11th, 2023 at 12:34 pm

    学会了!

    冰冰 August 17th, 2024 at 02:46 pm

    厉害了我的博主。

    rrvvaaaaov September 23rd, 2024 at 12:54 am

    叼茂SEO.bfbikes.com

    mkxqjkjdjt September 23rd, 2024 at 09:07 am

    想想你的文章写的特别好

    aexwphlqoy September 23rd, 2024 at 09:11 am

    看的我热血沸腾啊

    hnwsuiqqqv September 23rd, 2024 at 10:08 am

    看的我热血沸腾啊

    erxywqrrrq September 23rd, 2024 at 05:05 pm

    叼茂SEO.bfbikes.com

    pnrssredwo September 27th, 2024 at 12:37 pm

    怎么收藏这篇文章?

    jftkmiqtqs September 27th, 2024 at 12:40 pm

    怎么收藏这篇文章?

    xllnnnnpvp September 27th, 2024 at 02:04 pm

    怎么收藏这篇文章?

    rnbwxmtcqb October 1st, 2024 at 08:51 pm

    想想你的文章写的特别好https://www.237fa.com/

    idwhkwwtcw October 1st, 2024 at 08:55 pm

    看的我热血沸腾啊https://www.237fa.com/

    xaocsumyci October 1st, 2024 at 09:47 pm

    想想你的文章写的特别好https://www.237fa.com/

    usgafzmmcv October 4th, 2024 at 09:22 pm

    看的我热血沸腾啊https://www.ea55.com/

    csqlmagzth October 5th, 2024 at 08:07 pm

    看的我热血沸腾啊www.jiwenlaw.com

    popwfteysu October 6th, 2024 at 08:11 pm

    不错不错,我喜欢看 www.jiwenlaw.com