shell 如何使用bash获取变量中PID的内存使用情况

m528fe3b  于 2023-02-24  发布在  Shell
关注(0)|答案(1)|浏览(192)

这是我一直在研究的,但一直不成功

if [ "$memUsage" -gt "30500" ];
    then
        transUsage=$(pmap 3097 | tail -n 1 | awk '/[0-9]/{print $2}')
        transUsage=$transUsage | awk '/[0-9]/{print $2}' #This was my attempt at removing the extra K
        if [ "$transUsage" -gt "10500" ];
        then
        echo "Terminated this and this"
        fi
    # Print the usage
    echo "Memory Usage: $memUsage KB"
    fi

我需要变量中PID 3097的内存使用情况,以便使用if命令。当前它输出,

xxxxK, where x is memory usage size. Due to K being part of size, it's not being being recognized as numeric value.

如何解决这个问题?非常感谢您的帮助。问候!

9rbhqvlz

9rbhqvlz1#

您可以使用以下更好的代码:

#!/bin/bash

pid=$1

transUsage=$(pmap $pid | awk 'END{sub(/K/, "", $2); print $2}')
if ((transUsage > 10500)); then
    echo "Terminated this and this"
fi

echo "Memory Usage: $transUsage KB"

((...))是一个算术命令,如果表达式为非零,则返回退出状态0;如果表达式为零,则返回退出状态1。如果需要副作用(赋值),则也用作“let”的同义词。请参阅http://mywiki.wooledge.org/ArithmeticExpression

相关问题