查询某个进程使用资源情况

首先需要查到到进程,centos 使用top,iftop

#!/bin/bash

if [ $# -ne 1 ]; then
    echo "用法: $0 <进程ID>"
    exit 1
fi

pid=$1

if ! ps -p $pid &> /dev/null; then
    echo "进程 $pid 不存在"
    exit 1
fi

memory_info_KB=$(ps -p $pid -o rss --no-headers)

if [ -n "$memory_info_KB" ]; then
    memory_info_mb=$(echo "scale=2; $memory_info_KB / 1024" | bc)
    echo "进程 $pid 的内存使用情况(MB):"
    echo "$memory_info_mb"
fi
cpu_percent=$(ps -p $pid -o %cpu --no-headers)

if [ -n "$cpu_percent" ]; then
    echo "进程 $pid 的CPU使用情况:"
    echo "CPU 使用率: $cpu_percent%"
fi

io_info=$(cat /proc/$pid/io 2>/dev/null)

if [ -n "$io_info" ]; then
    read_Bytes_KB=$(grep "read_bytes" /proc/$pid/io | awk '{print $2 / 1024}')    # 将读取的字节数从字节转换为KB
    write_Bytes_KB=$(grep "write_bytes" /proc/$pid/io | awk '{print $2 / 1024}')  # 将写入的字节数从字节转换为KB
    read_ops=$(grep "syscr" /proc/$pid/io | awk '{print $2}')
    write_ops=$(grep "syscw" /proc/$pid/io | awk '{print $2}')

    echo "进程 $pid 的IO使用情况:"
    echo "读操作数: $read_ops"
    echo "写操作数: $write_ops"
    echo "读字节数(KB): $read_Bytes_KB"
    echo "写字节数(KB): $write_Bytes_KB"
fi

 

posted @ 2023-11-02 14:43  缺个好听的昵称  阅读(26)  评论(0编辑  收藏  举报