- Published on
WSL bashプロセスにおける合計のCPU使用率を出力する
- Authors
- Name
- Shou Arisaka / 有坂翔
Windows上で動作するLinuxパソコン・サーバーであるWSL Linux Bashにおいて、プロセスにおける合計のCPU使用率を出力する方法について紹介します。

Windwosにおいてのcpu使用率を確認するには以下コマンドで可能であることは以前の記事にも書いています。
# Check cpu usage for whole Windows 10 system
calc3 " round ( $( grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}' | chomplastchar ) ) "
しかしこんな場合はどうでしょうか。WSLのプロセスだけが使っているcpuの使用例を確認したい、という場合です。
# Check cpu usage within WSL system
calc3 " round ( ( $( printf -- "%s+" $( ps fux | awk 3 | tail -n +2 ) | chomplastchar ) ) ) "

使用している関数など
alias calc3='pythonprint3'
awk ()
{
: e.g. `# something | awk 2 `;
: e.g. `# something | awk nr2 `;
[[ "${1}" =~ ^[0-9]+$ ]] && {
/usr/bin/awk -v var="${1}" '{print $var}';
return 0
};
[[ "${1}" =~ ^nr[0-9]+$ ]] && {
/usr/bin/awk "NR==${1##+([a-z])}";
return 0
};
/usr/bin/awk "$@" && {
return 0
}
}
chomplastchar ()
{
chomp | python3 -c "import json,sys;print(sys.stdin.read()[:-1])"
}
pythonprint3 ()
{
python3 -c "print($1)"
}
chomp ()
{
perl -pe "chomp"
}