アプリケーションのメモリ/CPU使用率を取得する

アプリケーションのメモリ/CPU使用率を取得する

私は必要です

プロセスだけでなく、アプリケーションを通じてシステムリソース(メモリやCPU使用率など)を監視したいと思います。 Windowsタスクマネージャが「親プロセス呼び出し」を介してリソースをグループ化するのと同じように、私はこの方法を好みます。最近では、Firefoxやvscodeなどのアプリケーションが多くのサブプロセスを生成するので、そのアプリケーションの使い方をすばやく完全に理解したいと思います。

ソリューションはGUI、TUI、bashスクリプト、または大きな専門用語です。私は本当に気にしません。動作させるには、親プロセスのpidやフィルタリング手段として実行可能ファイルの名前を提供できると考えました。

はい

タスクマネージャはChromeブラウザシステムリソースをグループ化/蓄積します。

私が試したこと

  • 私はそれを試しましたが、htop呼び出しプロセスが呼び出すメモリではなく、独自のメモリを一覧表示するツリーのみを表示します。
  • やってみましたがgnome-system-monitor同じですね。
  • いくつか試してみましたが、必要に応じて機能させる正しいパラメータ/パイプラインセットが見つかりませんでしたpsfree

私を混乱させるのは、Googleで解決策を見つけることができないということです。理由があるのではないでしょうか?

誰でもどんなアイデアがありますか?とても感謝します!

ベストアンサー1

以下のスクリプトは追加の改善が必要ですが、基礎として機能できると思います。
レビューを書き始めましたが、まだ完成していません。余裕がある場合は、私の回答の編集内容を使用して新しいコメントを追加し、エラーを修正します。
私の環境ではうまくいきます。私はこのスクリプトの名前をmytopにし、/usr/local/binに入れてbashコマンドタブを完成させることができました。 mytop を ~/bin ディレクトリ ($PATH にない場合は ~/bin を追加) やコンピュータのどこにでも置くことができます。もちろん、実行ビットを設定するには、chmod u + x mytopを使用する必要があります。

#!/bin/bash
# mytop -ver 1.0

# script name (default is: 'mytop')
s_name=$(basename $0)

# version
ver="1.0"

# set default time between mytop iterations
sec_delay=3 
# set default mytop repetitions/iterations
mt_rep=1000000

# Help function explaining syntax, options, ...
Help()
{
# Display Help
echo
echo "Show Totals of %CPU and &MEM using 'top' command."
echo
echo "Syntax:"
echo "   $s_name [-h|-V]"
echo "   $s_name [[-d <S>][-n <N>] <APP_NAME>"]
echo 
echo "Options:"
echo "     -h       Print this Help."
echo "     -d S     Delay/wait S seconds between iterations (default: 3 seconds)."
echo "     -n N     Run/iterate 'mytop' N times (default: 3 times)."
echo "     -V       Print version."
echo
echo "Examples:"
echo "   mytop -V"
echo "   mytop -d1 -n5 chromium"
echo
echo 'Use CTRL+C for exit!'
echo
}

# Handling options from command line arguments
while getopts ":hn:d:V" option; do
case $option in
  h) # display Help
     Help
     exit;;
  V) # print version 
     echo "$s_name $ver"
     exit;;
  n) # set how many times 'mytop' will repeat/iterate
     mt_rep=$OPTARG;;
  d) # set delays in seconds
     sec_delay=$OPTARG;;
 \?)
     echo "$s_name: inapropriate: '$1'."
     echo "Usage:"
     echo "  $s_name [-h|-V|-d<S> -n<N> <APP_NAME>]"
     exit;;
  esac
done

# If no arguments given just display Help function and exit
if [[ $# -eq 0 ]]; then
    Help
    exit
else
    # If last argument starts with '-' exit from app
    if [[ ${@:$#} =~ -+.* ]]; then
        echo ${s_name}: error: Last argument must be the name of the application that you want to track. >&2
        exit 1
    else
        app_name=${@:$#}
    fi
fi



# Set 'dashes' literally
#t_dsh='-----------------------------------------------------------'
# or set them with printf command
t_dsh=$(printf '%0.s-' {1..59})

# Not in use
#if [[ -z $mt_rep ]] 2>/dev/null; then
#   r_endless=1
#   mt_rep=1000
#else
#   r_endless=0
#fi



i=0
while [[ $i -lt $mt_rep ]]; do

    #if [[ "$r_endless" == "0" ]]; then ((i++)); fi
    ((i++))

    # Handle pids of app you want to track by removing 'mytop' pids
    # get s_name (mytop) pids
    pgrep $s_name > /tmp/mt_pids
    # get app_name pids -all of them --not desired behaviour
    pgrep -f $app_name > /tmp/app_name_pids
    # get app_name without mytop pids --desired behaviour
    for e in $(cat /tmp/mt_pids); do sed -i "/$e/d" /tmp/app_name_pids; done
    if [[ ! -s "/tmp/app_name_pids" ]]; then echo "1000000" > /tmp/app_name_pids; fi

    # top -b -n1 -p; -b for output without ANSI formating; -n1 for just one iteration of 'top'; -p for feeding processes from 'pgrep' command
    # Use LC_NUMERIC if your 'top' command outputs 'commas' instead 'dots' - with LC_NUMERIC you will get 'dots' during this script
    LC_NUMERIC=en_US.UTF-8 top -b -n1 -p $(cat /tmp/app_name_pids | xargs | tr ' ' ,) > /tmp/pstemp

    wc_l=$(wc -l < /tmp/pstemp)

    cpu_use=$(tail -n +8 /tmp/pstemp | tr -s ' ' | sed 's/^ *//' | cut -d' ' -f9 | xargs | tr ' ' + | bc)
    if [[ "$cpu_use" == "0" ]]; then
        cpu_use="0.0"
    else
        if (( $(bc <<< "$cpu_use < 1") )); then cpu_use="0$cpu_use"; fi
    fi

    mem_use=$(tail -n +8 /tmp/pstemp | tr -s ' ' | sed 's/^ *//' | cut -d' ' -f10 | xargs | tr ' ' + | bc)
    if [[ "$mem_use" == "0" ]]; then
        mem_use="0.0"
    else
        if (( $(bc <<< "$mem_use < 1") )); then mem_use="0$mem_use"; fi
    fi

    echo -en "\033[2J\033[0;0f"
    # Use 'echo ...' above or 'tput ...' below (chose the one that works for you)
    #tput cup 0 0 && tput ed

    # Align Totals under %CPU and %MEM columns
    if (( $(bc <<< "$cpu_use < 1") )); then
        sed "${wc_l}a \\\n\nTotal (%CPU/%MEM): $(printf " %29s")$cpu_use  $mem_use\n${t_dsh}" /tmp/pstemp
    elif (( $(bc <<< "$cpu_use < 100") )); then
        sed "${wc_l}a \\\n\nTotal (%CPU/%MEM): $(printf " %28s")$cpu_use  $mem_use\n${t_dsh}" /tmp/pstemp
    else
        sed "${wc_l}a \\\n\nTotal (%CPU/%MEM): $(printf " %27s")$cpu_use  $mem_use\n${t_dsh}" /tmp/pstemp
    fi  

    if [[ $i -lt $mt_rep ]]; then sleep $sec_delay; fi
done

おすすめ記事