【问题标题】:Bash - Memory usageBash - 内存使用情况
【发布时间】:2012-05-30 14:22:24
【问题描述】:

我有一个我无法解决的问题,所以我来找你。

我需要编写一个程序来读取所有进程,并且程序必须按用户对它们进行排序,并且必须为每个用户显示使用了多少内存。

例如:

用户 1:120MB
用户 2:300MB
用户 3:50MB
总计:470MB

我正在考虑使用 ps aux 命令执行此操作,然后使用 awk 命令退出 pid 和用户。然后使用 pmap 我只需要获取进程的总内存使用量。

【问题讨论】:

标签: bash memory


【解决方案1】:

只是一点点更新,自动选择用户

#!/bin/bash
function mem_per_user {
    # take username as only parameter
    local user=$1
    # get all pid's of a specific user
    # you may elaborate the if statement in awk obey your own rules
    pids=`ps aux | awk -v username=$user '{if ($1 == username) {print $2}}'`

    local totalmem=0
    for pid in $pids
    do
        mem=`pmap $pid | tail -1 | \
            awk '{pos = match($2, /([0-9]*)K/, mem); if (pos > 0) print mem[1]}'`
        # when variable properly set
        if [ ! -z $mem ]
        then
            totalmem=$(( totalmem + $mem))
        fi
    done

    echo $totalmem
}

total_mem=0
for username in `ps aux | awk '{ print $1 }' | tail -n +2 | sort | uniq`
do
    per_user_memory=0
    per_user_memory=$(mem_per_user $username)
    if [ "$per_user_memory" -gt 0 ]
    then
       total_mem=$(( $total_mem + $per_user_memory))

       echo "$username: $per_user_memory KB"
    fi
done
echo "Total: $total_mem KB"

【讨论】:

  • 正如我从代码中看到的,这一定是它。但是有一个问题: awk: line 1: syntax error at or near ,这个显示了好几次,所以可能是循环中的awk错误(在函数中)?你能解决这个问题吗?非常感谢!问候
【解决方案2】:

试试这个脚本,它可能会解决你的问题:

#!/bin/bash
function mem_per_user {
    # take username as only parameter
    local user=$1
    # get all pid's of a specific user
    # you may elaborate the if statement in awk obey your own rules
    pids=`ps aux | awk -v username=$user '{if ($1 == username) {print $2}}'`

    local totalmem=0
    for pid in $pids
    do
        mem=`pmap $pid | tail -1 | \
            awk '{pos = match($2, /([0-9]*)K/, mem); if (pos > 0) print mem[1]}'`
        # when variable properly set
        if [ ! -z $mem ]
        then
            totalmem=$(( totalmem + $mem))
        fi
    done

    echo $totalmem
}

total_mem=0
for i in `seq 1 $#`
do
    per_user_memory=0
    eval username=\$$i
    per_user_memory=$(mem_per_user $username)
    total_mem=$(( $total_mem + $per_user_memory))

    echo "$username: $per_user_memory KB"
done
echo "Total: $total_mem KB"

最好的问候!

【讨论】:

  • Summer_More_More_Tea,感谢您提供此代码,但是当我尝试在终端中以 ./memory.sh 运行此代码时,上述脚本仅显示:总计:0KB。任何建议我做错了什么?
  • 不客气。你有指定用户名吗?该脚本将所有用户名作为由空格分隔的参数(`). For example, you want to check all the memory used by user summer_more_more_tea` 和root,命令为./memory.sh summer_more_more_tea root。希望有所帮助。:)
  • Summer_More_More_Tea,我没有给出用户名作为参数。但是我会尝试!非常感谢您的帮助! ;) 但是你能修改脚本,这样我就不需要将用户名作为参数了吗?猫 /etc/passwd | cut -d: -f1 --> 这会显示所有用户,所以我认为这会有所帮助。然后对于每个用户脚本,必须在您在该脚本中编写时显示内存使用情况。因为将脚本作为 ./memory.sh 运行至关重要,没有任何参数还有一件事,seq 1 $# 是什么意思?问候!
  • @golobich 抱歉我的回复晚了。我看到了@Tim 的修改,我认为它有效。除了catting /etc/passwd,您还可以通过wwhousers 获取有关登录用户的信息(仅供参考)。
【解决方案3】:

您可以使用 subprocess 模块在 python 中访问 shell 命令。它允许您生成子进程并连接到 out/in/error。可以执行ps -aux命令,用python解析输出。

check out the docs here

【讨论】:

  • 我无权使用 pythone 或任何其他语言。只是猛击。它必须全部用 bash 编写。你和另一个人认为。它必须具有 .sh 扩展名。这不会是一个问题。但我仍在寻找上一个问题的解决方案。谢谢
【解决方案4】:

这是我的版本。我认为 Tim 的版本不能正常工作,KB 中的值太大了。我认为应该使用 pmap -x 命令中的 RSS 列来提供更准确的值。但请注意,您不能总是获得正确的值,因为进程可以共享内存。阅读此A way to determine a process's "real" memory usage, i.e. private dirty RSS?

#!/bin/bash
 if [ "$(id -u)" != "0" ]; then
 echo "WARNING: you have to run as root if you want to see all users"
 fi
echo "Printing only users that current memmory usage > 0 Kilobytes "
all=0
for username in `ps aux | awk '{ print $1 }' | tail -n +2 | sort | uniq`
do
 pids=`ps aux | grep $username | awk -F" " '{print $2}'`
 total_memory=0
 for pid in $pids
 do
  process_mem=`pmap -x $pid | tail -1 | awk -F" " '{print $4}'`

  if [ ! -z $process_mem ] 
  then #don't try to add if string has no length
   total_memory=$((total_memory+$process_mem))
  fi
 done
#print only those that use any memmory
if [ $total_memory -gt 0 ]
then
 total_memory=$((total_memory/(1024)))
 echo "$username : $total_memory MB"
 all=$((all+$total_memory))
 fi
done
echo "----------------------------------------"
echo "Total: $all MB"
echo "WARNING: Use at your own risk"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-03
    • 2021-02-26
    • 2010-10-24
    • 2015-06-14
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多