0

解決できない問題があるので、あなたのところに来ました。

すべてのプロセスを読み取るプログラムを作成する必要があり、プログラムはユーザーごとに並べ替え、ユーザーごとにメモリの使用量を表示する必要があります。

例えば:

user1: 120MB
user2: 300MB
user3: 50MB
合計: 470MB

これを ps aux コマンドで実行してから、awk コマンドで pid と user を取得することを考えていました。次に、pmap を使用して、プロセスの合計メモリ使用量を取得するだけです。

4

4 に答える 4

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 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"

よろしくお願いします!

于 2012-05-24T03:34:42.567 に答える
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"
于 2012-05-24T20:04:59.563 に答える
0

subprocess モジュールを使用して、Python のシェル コマンドにアクセスできます。サブプロセスを生成し、out/in/error に接続できます。コマンドを実行しps -aux、Python で出力を解析できます。

こちらのドキュメントをご覧ください

于 2012-05-24T02:00:32.227 に答える